Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4337703
Votes 9
Synopsis URLClassLoader.getResource() fails (return null) on .jar file
Category java:classes_net
Reported Against 1.3
Release Fixed
State 11-Closed, Not Reproducible, bug
Priority: 4-Low
Related Bugs
Submit Date 12-MAY-2000
Description




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
   ClassLoader:jar:file:/E:/y/url.jar!/URLClassLoaderTest.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

WHAT IS WRONG ??

*/

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(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




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.
  xxxxx@xxxxx   2001-03-20
Comments
  
  Include a link with my name & email   

Submitted On 13-FEB-2001
AndyBaxman
This is a real pain! I'm using URLClassLoader to implement 
an Applet container. Trouble is, Applets that are Jar'd and 
use getResource are not portable to custom Applet viewers 
that use URLClassLoader! Anyone have any workarounds at all?


Submitted On 30-APR-2001
topcoMP
will it be fixed in any 1.3.x release ???


Submitted On 31-OCT-2002
gopeshgarg
This issue is happening again with  1.3.1_04 and above.


Submitted On 04-DEC-2002
leon.chadwick
I have found this behaviour in jdk1.2, 1.3 and 1.4.1_01.
It is intermittent, i.e. the same code could work one day and 
not the next.  The only workaround (and I only say this to 
help you sun people find and fix it), is to put your jar onto the 
bootclasspath, e.g. -Xbootclasspath/p:mycode.jar  on the 
java command line.  However that causes all sorts of other 
repercussions (though at least getResource() stops returning 
null).

Please fix this sun!!
leonchadwick@traderisks.com


Submitted On 06-DEC-2002
leon.chadwick
I have FOUND the solution (at least to why it happened for 
me)!!
The ClassLoader that loads files from the Windows filesystem 
is not case sensitive, however the .jar based classloader is.
Ensure that the filename of the file you want via getResource
() is exactly the same case as the actual string you pass into 
getResource().



PLEASE NOTE: JDK6 is formerly known as Project Mustang