Accessing JAR resources

前端 未结 4 1524
太阳男子
太阳男子 2020-11-30 10:47

I have a jar file with resources (mainly configuration for caches, logging, etc) that I want to distribute.

I\'m having a problem with the relative path

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 11:17

    I usually store files and other resources and then retrieve them as URLs:

    URL url = MyClass.class.getResource("/design/someResource.png");
    

    Within a static context, or otherwise:

    URL url = getClass().getResource("/design/someResource.png");
    

    From an instance.

    The above snippets assume that design is a top level folder within the jar. In general, if the path begins with a "/" it assumes an absolute path, otherwise it's relative from the class location.

提交回复
热议问题