Load a class from a jar

Viewed 639

I´m trying to load a class from a jar, I´m using a classLoader.

I have this parts of code for prepare the 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) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Now I load a class, and I try to get a new instance

....           
           loadClass();

           Class device = classLoader.loadClass( "org.myPackage.MyClass");

           MyMotherClass Device = ( MyMotherClass) device.newInstance();
...

MyClass extends of MyMotherClass, and when I do classLoader.loadClass( "org.myPackage.MyClass"), the MyMotherClass it is in the classLoader. At the moment, all right.

now, in device.newInstance(), I get a exception. The problem is the other classes that are used by MyClass, are not in the classpath.

What can i do?

I have a another method that load all the needed classes in the classLoader, but does not work when I get the new instance. I can not change MyClass and the others

1 Answers
Related