mardi 28 février 2017

Load a java class dynamically

I have a classLoader file located in the /root directory. It is not contained inside any package. Inside the classLoader file I'm trying to load a class file "test", located in "com.sample.demo" package. The com folder is also present in /root directory.

public class classLoader{
 public static void main(String[] args)  InvocationTargetException
  {
    Class c = Class.forName("com.sample.demo.test");
  }
}

I compile and run from the root directory as,

javac classLoader.java
java classLoader 

I get the following Exception,

Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/demo/test (wrong name: test)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at classLoader.main(classLoader.java:13)





Aucun commentaire:

Enregistrer un commentaire