Skip to main content

my JarLoader breaks when replacing the jar file

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2006-10-06


I have created a JarLoader class to load jar file at runtime. I want to keep different versions of jar files so I cache the JarLoader in a Map. The problem is when a jar file is loaded and I replace the jar file with a new file, the JarLoader can't find resources inside the jar file and it returns null.

It is strange because it was working before and all of a sudden it changed!!

Here is my jar loader:


  1. public class JarResourceLoader extends URLClassLoader {
  2. public JarResourceLoader(String spec) throws MalformedURLException {
  3. super(new URL[] {new URL(spec)});
  4. }
  5. protected void addURL(URL url) {
  6. super.addURL(url);
  7. }
  8. public void addJar(String path) throws MalformedURLException {
  9. String url = "jar:file:/" + path + "!/";
  10. addURL(new URL(url));
  11. }
  12. public void addFilePath(String path) throws MalformedURLException {
  13. addURL(new URL("file:" + path + ""));
  14. }
  15. }
  16. </code>

The following is the test case I wrote:

  1. String jar = "file:c:\\\\classpath/my-resources-0.0.1-SNAPSHOT.jar";
  2. JarResourceLoader loader = new JarResourceLoader(jar);
  3. InputStream is = loader.getResourceAsStream("META-INF/resources/css/template.css");
  4. System.out.println(is.available());
  5. InputStream is2 = loader.getResourceAsStream("META-INF/resources/css/template.css");
  6. System.err.println(is2.available());
  7. InputStream is3 = loader.getResourceAsStream("META-INF/resources/css/template.css");
  8. System.out.println(is3.available());
  9. </code>

I put a breakpoint on "is2" and replaced the jar with new one, and is2 then returned null and the same result for is3

I am wondered how come it was working before!

I also tested this URL "jar:file:/c:\\classpath/my-resources-0.0.1-SNAPSHOT.jar!/" isntead but no change!

Any idea is appreciated.