1

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();
        }
    }
}

2 Answers 2

0

What I have done in the past is to load the classes by calling the defineClass method of the current class loader (using reflection)

http://sourceforge.net/projects/essence/files/Essence%20Java%20Config.%20Files/Essence%20JCF%201.02/

Sign up to request clarification or add additional context in comments.

2 Comments

I can't see the source code. Could you possibly link or show it?
The source can be downloaded from that link but it can also be browsed essence.svn.sourceforge.net/viewvc/essence/trunk/essence-file/…
0

This page had exactly what I was looking for:

http://jimlife.wordpress.com/2007/12/19/java-adding-new-classpath-at-runtime/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.