I am trying to load a class from jar, I am using classLoader.
I have this piece of code for preparing classLoader:
private void loadClass(){
try{
JarFile jarFile = new JarFile( Path);
Enumeration e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + Path +"!/") };
classLoader = URLClassLoader.newInstance(urls);
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Now I am loading the class and I am trying to get a new instance
....
loadClass();
Class device = classLoader.loadClass( "org.myPackage.MyClass");
MyMotherClass Device = ( MyMotherClass) device.newInstance();
...
MyClass extends MyMotherClass, and when I execute the Loader.loadClass ("org.myPackage.MyClass") class, MyMotherClass is in classLoader. At the moment, everything is in order.
Now, in device.newInstance (), I get an exception. The problem is that the other classes used by MyClass are not in the classpath.
What can I do?
I have another method that loads all the necessary classes into classLoader, but does not work when I get a new instance. I can not change MyClass and others.
Clonw source
share