As part of the resources in the JAR

We have a jar file. There are 3 folders:

1st: META-INF

2nd: resources

3rd: classes

How does a class from the class folder get an image from the resource folder?

+4
source share
2 answers

Here is an example:

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

To do this in a static context, for example, in a static initializer block:

 String path = "resources/something.png"; BufferedImage img = ImageIO.read(className.class.getClassLoader().getResource(path)); 
+2
source

You want to use ClassLoader.getResource or getResourceAsStream , both of which will allow you to read files stored in your JAR. You can access the class loader using YourClass.class.getClassLoader() . See this question for more information: Download the resource contained in the bank

+1
source

Source: https://habr.com/ru/post/1338534/


All Articles