2012-05-07 13 views
1

由於Java ClassNotFoundException when reading object from a stream的Java添加程序編譯的類可用引用

我已經添加了服務器收到的java文件的能力,它可以編譯它擴展當前類的列表。但是,我仍然得到相同的ClassNotFoundException。如何讓服務器查看並使用它編譯的類從objectInputStream中讀取對象?

代碼閱讀和編寫關於Serverside集團的.java:

private void doNewWorkUnitDefinition(Object object) { 
    NewWorkUnitDefinition newWorkUnitDefinition = (NewWorkUnitDefinition)object; 
    byte[] bytes = newWorkUnitDefinition.getBytes(); 
    String name = newWorkUnitDefinition.getName(); 

    if (isClient) 
     client.gotNewWorkUnitDefinition(bytes, name); 
    else { 
     String executionPath = System.getProperty("user.dir"); 
     File file = new File(executionPath, name); 
     try { 
      FileOutputStream fileOut = new FileOutputStream(file); 
      fileOut.write(bytes); 
      fileOut.close(); 
      System.out.println("Just wrote a file to: " + file.getAbsolutePath()); 

      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 
      compiler.run(System.in, System.out, System.err, file.getAbsolutePath()); 

      ClassLoader cl_old = Thread.currentThread().getContextClassLoader(); 
      ClassLoader cl_new = new URLClassLoader(new URL[] { file 
        .getParentFile().toURI().toURL() }, cl_old); 
      System.out.println("WorkUnitFile: " + file.getAbsolutePath()); 
      Class compiledClass = cl_new.loadClass(file.getName() 
        .replace(".java", "")); 
      this. 

      sendConfirmDefinitionReceipt(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答