1

从流中读取对象时作为 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();
        }
    }
}
4

2 回答 2

0

我过去所做的是通过调用当前类加载器的defineClass方法来加载类(使用反射)

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

于 2012-05-07T12:52:22.143 回答
0

这个页面正是我想要的:

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

于 2012-05-07T23:14:04.080 回答