user2032306
user2032306

Reputation: 21

Loading static resources from inside one-jar

I have a web page hosted on jetty server. The page displays some static images, the images directory are kept inside a jar, say application.jar. This application.jar is packaged inside a one-jar. I am facing problem in accessing the images directory as they are inside a jar which is inside another jar. I have tried almost all the ways to get URL of the images directory: class.getClassLoader().getResource(), Thread.currenctThread.getContextClassLoader().getResource(), ClassLoader.getSystemClassLoader().getResource(), etc. None of them is of any help. I wrote all these statements, i.e. tried to get access to the images directory, from a class that is inside the application.jar. This jar contains the images directory too.

If anybody has ever faced this before, please reply to this thread. I am open to any other ideas also that may help me achieve the objective.

Upvotes: 2

Views: 1563

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365068

You can use

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");

You can find more here:

How to read a file from jar in Java?

How to access resources in JAR file?

Upvotes: 2

Related Questions