Within resources in JAR
We have a Jar-file. There are 3 folders:
1-st: META-INF
2-nd: resources
3-rd: classes
How can a class from folder classes get image from fold开发者_JS百科er resources?
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));
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
精彩评论