JaeR
JaeR

Reputation: 63

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 folder resources?

Upvotes: 4

Views: 1290

Answers (2)

ide
ide

Reputation: 20838

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

Upvotes: 1

Matt Eskridge
Matt Eskridge

Reputation: 1030

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));

Upvotes: 2

Related Questions