dcsimg
Using URLClassLoader with JarURLConnection URLs (e.g. "jar:...")
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Donnie_Hale
Posted On:   Thursday, December 27, 2001 11:52 AM

The short form of my question is simply: Should URLClassLoader as it comes in JDK 1.3 be able to find and load classes if it's instantiated using URLs of the form "jar:file:myjar.jar!/whatever" (the form described in the JarURLConnection javadocs)? I'm getting a ClassNotFoundException when trying to do a loadClass on a class in a jar that's in another jar using an URLClassLoader instantiated with several URLs of the form mentioned. The longer form is this: I'm trying to set up a classloader that acts for a regular java app the way, for example, a servlet container does - allowing classes to be loaded from a jar file that's in a "directory" inside the application .jar file. So I'm getting the application    More>>

The short form of my question is simply:


Should URLClassLoader as it comes in JDK 1.3 be able to find and load classes if it's instantiated using URLs of the form "jar:file:myjar.jar!/whatever" (the form described in the JarURLConnection javadocs)? I'm getting a ClassNotFoundException when trying to do a loadClass on a class in a jar that's in another jar using an URLClassLoader instantiated with several URLs of the form mentioned.


The longer form is this: I'm trying to set up a classloader that acts for a regular java app the way, for example, a servlet container does - allowing classes to be loaded from a jar file that's in a "directory" inside the application .jar file. So I'm getting the application jar's manifest, reading the classpath attribute, and forming the "jar:" URLs for each entry in that classpath attribute. Then I instantiate an URLClassLoader with that array of "jar:" URLs. But when I try to load a class I know is in the "inner" jar file, I'm getting the exception.


Should this work? I saw a message about problems with compressed jar files, and all the jar files I'm using are uncompressed.


Thanks,


Donnie

   <<Less

Re: Using URLClassLoader with JarURLConnection URLs (e.g. "jar:...")

Posted By:   Christopher_Schultz  
Posted On:   Friday, December 28, 2001 05:07 AM

I'm not sure why the URLClassLoader is refusing to load your classes.



Your URL should be something like jar:file:myjar.jar!/, and have no path information. The root of the JAR file should be the root of that part of the classpath. That way, when I try to load com.xyz.MyClass, it'll look in your JAR under the directory com/xyz for MyClass.



If this doesn't work, you can easily write your own classloader to read classes from a JAR file. You just need to worry about getting the bytes of the file, then calling resolve, etc.



The only trick is that you need to make sure that the VM is actually using your classloader instead of the default classloader.



-chris
About | Sitemap | Contact