My problem is the following:
I am trying to access a class file in a Jar.
I have a jar “dumb.jar” located in package “com.msgxsd”. The jar is on the classpath, and If I try and do
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("com/msgxsd/dumb.jar");
then I get an input stream for the jar without a problem.
The problem arises when I try and get an input stream (or a URL) for the file inside the jar. All the jar contains (apart from the manifest obviously) is the file “DummyClass1.class”.
I have tried all of the following:
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("DummyClass1.class");
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("dumb.jar!/DummyClass1.class");
But the input stream returned is always null. I have also tried
URL url = this.getClass().getResource("DummyClass1.class");
URL url = this.getClass().getResource("dumb.jar!/DummyClass1.class");
But the URL returned is null.
I have already looked at every single one of the questions relating to accessing a file within a Jar, but the proposed solutions (as seen above) do not work for me.
All I want is to be able to get either an InputStream (or ideally a URL) for the “DummyClass1.class” inside the jar, so that I can the somehow construct a File instance.
Any suggestions would be much appreciated.
Try this (sorry if there are typos, I’m just typing the code off the top of my head without trying it):