f



Creating custom ClassLoader.

Hello All,

    I am trying to create my own classloader that will load a different 
version of a class from a jar file.  I create a new thread and subclass 
URLClassloader and override the loadClass method such that it will 
search in the given jar first for the class and return that.

    Tracing through the execution all seems to go well.  The loadClass 
method returns what seems to be a valid class but when I attempt to use 
the class I receive an InstantiationExeception.  I have confirmed that 
the URL is valid so I know that it is not an issue.  I also created a 
wide open java.policy file just in case it was something security related.

    The relevent code is below.  I'd appreciate any pointers that anyone 
can give.

Cheers


****************************************
Relevent portion of thread code
****************************************

public final void run() {
     URL[] codeLLocation = new URL[1];
     codeLLocation[0] = codeLocation;
     NewURLClassLoader loader = new NewURLClassLoader(codeLLocation);
     Thread.currentThread().setContextClassLoader(loader);
     Object o = null;
     while (keepRunning) {
         if (o == null) {
             try {
System.out.println("The parent classloader = " + loader.getParent());
Class c = loader.loadClass("<full_package_name_of_object");
System.out.println("Classloader = " + c.getClassLoader());
                 Object o = c.newInstance(); <=====  Exception here.


****************************************
Extended URLClassLoader
****************************************

package org.fhtc.jde.threadtest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class NewURLClassLoader extends URLClassLoader {
     URL[] urls;
     NewURLClassLoader(URL[] inputURLS) {
         super(inputURLS);
         urls = inputURLS;
     }
     public Class findClass(String name) throws ClassNotFoundException {
         byte[] b = loadClassData(name);
         if (b == null) {
             throw (new ClassNotFoundException());
         }
         Class c = super.defineClass(name, b, 0, b.length);
         return c;
     }
     public synchronized Class loadClass(String name)
                     throws ClassNotFoundException {
         // First, check if the class has already been loaded
         Class c = findLoadedClass(name);
         if (c == null) {
             try {
                 c = this.findClass(name);
             } catch (ClassNotFoundException e) {
                 c = super.loadClass(name);
             }
         }
         return c;
     }
     private byte[] loadClassData(String className) {
         byte[] buffer = new byte[1];
         String realName = className.replace('.', '/');
         realName += ".class";
         try {
             JarFile jarFile = new JarFile(urls[0].getFile());
             JarEntry entry = jarFile.getJarEntry(realName);
             if (entry != null) {
                 buffer = new byte[(int) entry.getSize()];
                 BufferedInputStream is = new BufferedInputStream(jarFile
                                 .getInputStream(entry));
                 is.read(buffer);
             } else {
                 buffer = null;
             }
         } catch (IOException e) {
             buffer = null;
         }
         return buffer;
     }
}
0
junk763 (106)
6/18/2004 10:40:17 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

3 Replies
263 Views

Similar Articles

[PageSpeed] 22

On Fri, 18 Jun 2004 18:40:17 -0400, Frank Rouse <junk@junk.net> wrote
or quoted :

>        Object o = c.newInstance(); <=====  Exception here.

does the class have a no arg public constructor?

-- 
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming. 
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
0
look-on (4215)
6/18/2004 10:54:35 PM
Roedy Green wrote:

> On Fri, 18 Jun 2004 18:40:17 -0400, Frank Rouse <junk@junk.net> wrote
> or quoted :
> 
> 
>>       Object o = c.newInstance(); <=====  Exception here.
> 
> 
> does the class have a no arg public constructor?
> 

    Thanks for the reply.  I did create a no parameter constructor just 
for testing purposes.  Still no luck.  Open to other suggestions.

Cheers
0
junk763 (106)
6/19/2004 4:03:49 AM
Frank Rouse wrote:
> >>       Object o = c.newInstance(); <=====  Exception here.

[...]

>     Thanks for the reply.  I did create a no parameter constructor just 
> for testing purposes.  Still no luck.  Open to other suggestions.

Frank,

I missed your original post (my server has been acting up recently), but 
I don't see a mention of what exception you're getting.  There are 
several exceptions that can be thrown by Class.newInstance(), and they 
indicate different problems.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
0
cdsmith (3862)
6/20/2004 1:31:07 AM
Reply: