2

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.

  • 1
    Please show what you've tried, and the layout of the jar file. – Jon Skeet Feb 01 '13 at 11:58
  • Using getResource() you need get an input stream to the second jar, then use a JarInputStream to iterate over JarEntries. If you have control over the packaging, I would not recommend putting a JAR into a JAR. – rudolfson Feb 01 '13 at 12:07

1 Answers1

2

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?

Community
  • 1
  • 1
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841