M
Matthew Keene
I'm trying to dynamically add some libraries to the application class
path by using a URLClassLoader, but it doesn't seem to be behaving the
way that I want it to. I have to classes as follows:
import java.lang.reflect.* ;
import java.io.* ;
import java.net.* ;
public class ClassLoaderTestParent {
public ClassLoaderTestParent() {
}
public static void main(String[] args) throws Exception {
File f = new
File("H:/Data/Java/ExceptionReportEngine/gnu-regexp-1.1.4.jar") ;
URL u = f.toURL() ;
URL[] urls = {u} ;
URLClassLoader ucl = new URLClassLoader(urls) ;
Class myClass = ucl.loadClass("ClassLoaderTestChild") ;
// Also tried using forName, as below
//Class myClass = Class.forName("ClassLoaderTestChild",true,ucl) ;
Object myInstance = myClass.newInstance() ;
Class[] methodArgClass = {} ;
Method myMethod = myClass.getMethod("childMethod",methodArgClass) ;
Object[] methodArgVals = {} ;
System.out.println("About to invoke method on dynamically loaded
class") ;
Object result = myMethod.invoke(myInstance,methodArgVals) ;
}
}
This class will create a URL class loader with the specified jar file
added to the classpath, then load the desired class, instantiate it
and then dynamically invoke a method on the instance. The class which
is invoked is shown below
import gnu.regexp.* ;
public class ClassLoaderTestChild {
public ClassLoaderTestChild() {
}
public String childMethod() throws Exception {
System.out.println("Inside child method") ;
System.out.println("Class path is " +
System.getProperty("java.class.path")) ;
RE testRE = new RE("abcde") ;
return "Hello" ;
}
}
When I run this, I expect that the GNU regexp libraries should be
available to the child class, as I added them to the class loader in
the parent. Instead, I get a NoClassDefFoundError, as shown below
H:\Data\Java>java -classpath . ClassLoaderTestParent
About to invoke method on dynamically loaded class
Inside child method
Class path is .
Exception in thread "main"
java.lang.reflect.InvocationTargetException:
java.lang.NoClassDefFoundError: gnu/regexp/RE
at ClassLoaderTestChild.childMethod(ClassLoaderTestChild.java:12)
at java.lang.reflect.Method.invoke(Native Method)
at ClassLoaderTestParent.main(ClassLoaderTestParent.java:24)
Can anybody explain this behaviour, and even better, tell me what I
would have to do to get the behaviour I'm after (ie to have the
dynamically added class libraries available to the loaded class) ?
TIA
Matthew
path by using a URLClassLoader, but it doesn't seem to be behaving the
way that I want it to. I have to classes as follows:
import java.lang.reflect.* ;
import java.io.* ;
import java.net.* ;
public class ClassLoaderTestParent {
public ClassLoaderTestParent() {
}
public static void main(String[] args) throws Exception {
File f = new
File("H:/Data/Java/ExceptionReportEngine/gnu-regexp-1.1.4.jar") ;
URL u = f.toURL() ;
URL[] urls = {u} ;
URLClassLoader ucl = new URLClassLoader(urls) ;
Class myClass = ucl.loadClass("ClassLoaderTestChild") ;
// Also tried using forName, as below
//Class myClass = Class.forName("ClassLoaderTestChild",true,ucl) ;
Object myInstance = myClass.newInstance() ;
Class[] methodArgClass = {} ;
Method myMethod = myClass.getMethod("childMethod",methodArgClass) ;
Object[] methodArgVals = {} ;
System.out.println("About to invoke method on dynamically loaded
class") ;
Object result = myMethod.invoke(myInstance,methodArgVals) ;
}
}
This class will create a URL class loader with the specified jar file
added to the classpath, then load the desired class, instantiate it
and then dynamically invoke a method on the instance. The class which
is invoked is shown below
import gnu.regexp.* ;
public class ClassLoaderTestChild {
public ClassLoaderTestChild() {
}
public String childMethod() throws Exception {
System.out.println("Inside child method") ;
System.out.println("Class path is " +
System.getProperty("java.class.path")) ;
RE testRE = new RE("abcde") ;
return "Hello" ;
}
}
When I run this, I expect that the GNU regexp libraries should be
available to the child class, as I added them to the class loader in
the parent. Instead, I get a NoClassDefFoundError, as shown below
H:\Data\Java>java -classpath . ClassLoaderTestParent
About to invoke method on dynamically loaded class
Inside child method
Class path is .
Exception in thread "main"
java.lang.reflect.InvocationTargetException:
java.lang.NoClassDefFoundError: gnu/regexp/RE
at ClassLoaderTestChild.childMethod(ClassLoaderTestChild.java:12)
at java.lang.reflect.Method.invoke(Native Method)
at ClassLoaderTestParent.main(ClassLoaderTestParent.java:24)
Can anybody explain this behaviour, and even better, tell me what I
would have to do to get the behaviour I'm after (ie to have the
dynamically added class libraries available to the loaded class) ?
TIA
Matthew