0

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");
Noomak
  • 371
  • 5
  • 19
  • http://stackoverflow.com/questions/11016092/how-to-load-classes-at-runtime-from-a-folder-or-jar http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime http://stackoverflow.com/questions/16104605/dynamically-loading-jar-and-instanciate-an-object-of-a-loaded-class – Emerson Cod Oct 23 '15 at 13:10
  • You seem to replace .java extension with .class extension.Seriously,this is not java compilation.A java class is in byte code compiled by a java compiler – Kumar Abhinav Oct 23 '15 at 13:23
  • @KumarAbhinav I know thanks, infact that you are mentioning is a class loading not a java file compiling (operaration that I did before that piece of code) – Noomak Oct 23 '15 at 14:05
  • I need to load a .class not a .class in a .jar – Noomak Oct 23 '15 at 14:06

1 Answers1

1

When you specify a class path you have to provide the directory which is the parent to all your packages. Try instead.

 new URL("file:///"+UtilOverriding.getFile1().getParent()}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130