JDK-4337703 : URLClassLoader.getResource() fails (return null) on .jar file
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-05-12
  • Updated: 2001-03-21
  • Resolved: 2001-03-21

Name: rlT66838			Date: 05/11/2000

Classic VM (build JDK-1.2.2-001, native threads, symcjit)

URLClassLoader.getResource() FAIL on .jar file (return null with .jar file)
JDK 1.2.2-001 (Windows98 and Linux), jdk 1.3 Windows

1. Put this file into some work directory (for example y)
2. Create file 'x' in the same directory (y)
3. Compile
   javac -classpath . URLClassLoader.java
4. Run test from directory
   java -cp . URLClassLoader

   RESULT OK (resource 'x' founded):
   Resource 'x' by Class ClassLoader:file:/E:/y/x
   Class file by Class ClassLoader:file:/E:/y/URLClassLoaderTest.class
   URLClassLoader URL: file:/E:/y/
   Resource 'x' by URLClassLoader:file:/E:/y/x
   Class file by URLClassLoader:file:/E:/y/URLClassLoaderTest.class

5. Pack files 'URLClassLoader.class' and 'x' into jar
   jar cvf url.jar URLClassLoader.class x

6. Run test from within jar file
   java -cp url.jar URLClassLoader

   RESULT BAD (resource 'x' not found)
   Resource 'x' by Class ClassLoader:null
   Class file by Class
   URLClassLoader URL: file:/E:/y/url.jar!/
   Resource 'x' by URLClassLoader:null
   Class file by URLClassLoader:jar:file:/E:/y/url.jar!/URLClassLoaderTest.class



import java.net.*;

public class URLClassLoaderTest
	public static void main(String[] args)
      URLClassLoaderTest test = new URLClassLoaderTest();
      // reference class (point to resources)
      Class where = test.getClass();
      URL u = where.getResource("x");  // OK with local filesystem, null with .jar
      URL [] url = {where.getResource("URLClassLoaderTest.class")};
        System.out.println("Resource 'x' by Class ClassLoader:" + u);
		System.out.println("Class file by Class ClassLoader:" + url[0]);

      String s = url[0].getFile();
      int k = s.lastIndexOf("/");
      s = s.substring(0, k + 1);
      try {
      	url[0] = new URL(url[0], s);
      // Now url[0] point to directory with class file (and 'x' file)
      } catch (Exception e) {
     System.out.println("URLClassLoader URL: " + url[0]);
      // create ClassLoader
      URLClassLoader ucl = new URLClassLoader(url);
      u = ucl.getResource("x"); // OK with local filesystem, null with .jar file
		System.out.println("Resource 'x' by URLClassLoader:" + u);
      u = ucl.getResource("URLClassLoaderTest.class");
		System.out.println("Class file by URLClassLoader:" + u);
(Review ID: 104729) 

WORK AROUND Name: rlT66838 Date: 05/11/2000 No work Around ======================================================================

EVALUATION This was an issue in 1.3 and ladybird but in merlin getResource is returning the executed file:/dir/url.jar!/x Based on this I'm closing this bug. alan.bateman@ireland 2001-03-20