0

I am new to class Loading, so please help me if taking anything wrong.

Exception in thread "main" java.lang.ClassNotFoundException: Hello at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

class TestClassLoader extends ClassLoader {
 @Override
 public Class<?> loadClass(String name) throws ClassNotFoundException {
     /*if (name.equals("test.Test1"))*/ {
         try {
             InputStream is = Object.class.getClassLoader().getResourceAsStream(name);
             byte[] buf = new byte[10000];
             int len = is.read(buf);
             return defineClass(name, buf, 0, len);
         } catch (IOException e) {
             throw new ClassNotFoundException("", e);
         }
     }
     /*return getParent().loadClass(name);*/
 }
}

public class DynamicCompilerTest {
    public static void main(String[] args) throws Exception {

        String fullName = "Hello";

        // Here we get and specify the source code of the class to be compiled     
        String formuae = "b+a";
        String src = "import I1; public class Hello implements I1 { public int calculate(int a, int b){return "+formuae+";}   public String toString2() {        return \"Hello, I am \"  ;   }}";

        DynamicCompile uCompiler = new DynamicCompile(fullName, src);
        uCompiler.compile();
        I1 aa = (I1) uCompiler.run();
        System.out.println(aa.toString2());
        System.out.println(aa.calculate(5, 5));
        //Hashtable<String, I1> ht = new Hashtable<>();
        MyMap<String, I1> mm = new MyMap<>();
        mm.put("hello", aa);

        myThread th = new myThread();
        th.setHt(mm);
        th.start()  ;
       Thread.sleep(50);
        String src2 = "import I1; public class Hello implements I1 { public int calculate(int a, int b){return a*b;}   public String toString2() {        return \"Hello, I am NEW \"  ;   }}";
      try{ mm.writerLock(); 
             Thread.sleep(2000);
            DynamicCompile uCompiler2 = new DynamicCompile(fullName, src2);
            uCompiler2.compile();

            I1 aa2 = (I1) uCompiler2.run();
            System.out.println(aa2.toString2());
            System.out.println(aa2.calculate(5, 5));
            }   
        finally{
            mm.releaseWriteLock();    
        }
    }
}
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
sumit
  • 1
  • 1
  • Where does `DynamicCompile` come from exactly? – Sean Bright Jul 28 '16 at 16:29
  • trying to use javax.tools.*; to dynamically compile code. – sumit Aug 01 '16 at 10:08
  • OK, but `DynamicCompile` is not a class in `javax.tools` – Sean Bright Aug 03 '16 at 17:57
  • Besides the fact that you posted the stack trace of a `ClassNotFoundException` instead of a `NullPointerException`, you should know that `Object.class.getClassLoader()` returns `null` which represents the bootstrap loader. However, it’s pointless to write a custom class loader searching in the scope of the system class loader anyway. And apparently, you’re not using it at all. – Holger Aug 31 '16 at 17:35

0 Answers0