3

我正在尝试使用 javax.tools 编译字符串中的代码的代码示例。Class.forName 导致 ClassNotFoundException。有人知道为什么吗?我正在使用 Java 7。

        import java.io.IOException;
        import java.io.PrintWriter;
        import java.io.StringWriter;
        import java.lang.reflect.InvocationTargetException;
        import java.net.URI;
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.List;

        import javax.tools.Diagnostic;
        import javax.tools.DiagnosticCollector;
        import javax.tools.JavaCompiler;
        import javax.tools.JavaFileObject;
        import javax.tools.SimpleJavaFileObject;
        import javax.tools.ToolProvider;
        import javax.tools.JavaCompiler.CompilationTask;


        public class Main {
            public static void main(String args[]) throws IOException {
                JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
                DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

                StringWriter writer = new StringWriter();
                PrintWriter out = new PrintWriter(writer);
                out.println("public class HelloWorld {");
                out.println("  public static void main(String args[]) {");
                out.println("    System.out.println(\"This is in another java file\");");
                out.println("  }");
                out.println("}");
                out.close();
                JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString());

                Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
                CompilationTask task = compiler.getTask(null, null, diagnostics, optionList, null, compilationUnits);

                boolean success = task.call();
                for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
                    System.out.println(diagnostic.getCode());
                    System.out.println(diagnostic.getKind());
                    System.out.println(diagnostic.getPosition());
                    System.out.println(diagnostic.getStartPosition());
                    System.out.println(diagnostic.getEndPosition());
                    System.out.println(diagnostic.getSource());
                    System.out.println(diagnostic.getMessage(null));

                }
                System.out.println("Success: " + success);

                if (success) {
                    try {
                        Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class })
                                .invoke(null, new Object[] { null });
                    } catch (ClassNotFoundException e) {
                        System.err.println("Class not found: " + e);
                    } catch (NoSuchMethodException e) {
                        System.err.println("No such method: " + e);
                    } catch (IllegalAccessException e) {
                        System.err.println("Illegal access: " + e);
                    } catch (InvocationTargetException e) {
                        System.err.println("Invocation target: " + e);
                    }
                }
            }
        }

        class JavaSourceFromString extends SimpleJavaFileObject {
            final String code;

            JavaSourceFromString(String name, String code) {
                super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
                this.code = code;                                                              
            }

            @Override
            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                return code;
            }
        }
4

1 回答 1

0

类加载器不知道你的HelloWorld类。你可以这样做:

URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() }); //root is path to class file
Class<?> cls = Class.forName("HelloWorld", true, classLoader); 

您可以在此处查看完整示例

于 2013-08-09T10:56:29.057 回答