Within resources in JAR

后端 未结 2 389
星月不相逢
星月不相逢 2020-12-21 02:47

We have a Jar-file. There are 3 folders:

1-st: META-INF

2-nd: resources

3-rd: classes

How

相关标签:
2条回答
  • 2020-12-21 03:00

    You want to use ClassLoader.getResource or getResourceAsStream, both of which will allow you to read files stored within your JAR. You can access the class loader with YourClass.class.getClassLoader(). See this question for more details: Load a resource contained in a jar

    0 讨论(0)
  • 2020-12-21 03:24

    Here's an example:

    String path = "resources/something.png";
    BufferedImage img = ImageIO.read(getClass().getClassLoader().getResource(path));
    

    To do it in a static context, like in a static initializer block:

    String path = "resources/something.png";
    BufferedImage img = ImageIO.read(className.class.getClassLoader().getResource(path));
    
    0 讨论(0)
提交回复
热议问题