从流中读取对象时作为 Java ClassNotFoundException 的扩展
我添加了服务器接收 .java 文件的功能,它可以编译它。但是,我仍然得到相同的 ClassNotFoundException。如何让服务器看到并使用它编译的类从 objectInputStream 中读取对象?
在服务器端读取和编译 .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();
}
}
}