As an extension of Java ClassNotFoundException when reading object from a stream
I have added the ability for the server to recieve the .java file, and it can compile it. However, I am still getting the same ClassNotFoundException. How can I make the server see and use the class it compiled to read the object from the objectInputStream?
Code to read and compile the .java on the serverSide:
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();
}
}
}