vendredi 23 octobre 2015

How to load .class and call one of its methods

I've compiled at runtime a java file on file system which is a very simple class:

public class Test {

public static int res(int a, int b) {   
    return a*b;
}   

}

now I just want to call res method from my project (that will be a jar), but my code produce java.lang.ClassNotFoundException: Test

and this is how I've loaded the class:

URL[] urls = new URL[] {new URL("file:///"+UtilOverriding.getFile1().replace("java", "class"))};
        URLClassLoader loader = new URLClassLoader(urls);
        Class clazz = loader.loadClass("Test");





Aucun commentaire:

Enregistrer un commentaire