Compiling Java code from string results in ClassNotFoundException

2.1k Views Asked by At

I'm trying a code example that uses javax.tools to compile code that is in a string. The Class.forName results in a ClassNotFoundException. Does anybody know why? I'm using 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;
            }
        }
2

There are 2 best solutions below

2
On

The classloader has no idea of your HelloWorld class. You can do this:

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

You can see a full example here.

0
On

I faced the same problem today.
Upon checking, I added a breakpoint in URLClassLoader.java on this block:

String path = name.replace('.', '/').concat(".class");
Resource res = ucp.getResource(path, false);
if (res != null) {
    try {
        return defineClass(name, res);
    }

If a path to a file is incorrect, you will get ClassNotFoundException in line

Resource res = ucp.getResource(path, false);

If name and res have different name structures, you will get NoClassDefFoundError in line

return defineClass(name, res);

This is a tricky one! You need name and res be in a same form. For example:
name - com.qwerty.MyClass
res - com/qwerty/MyClass.class
So you have to ensure that ucp references a location of a package (not of a class).

So make sure that:

  1. URLClassLoader.newInstance({srcDir.toURI().toURL()})
    srcDir points to a directory where the generated package is located.
  2. Class.forName(generatedPackageName + "." + generatedClassName)
    You feed the Class.forName with a full class name, including the package name.

Working example:

String fileSeparator = File.separator;
String generatedPackageName = "com.qwerty.generated";
String generatedClassName = "MyClass"

// Save source in .java file
File srcDir = new File("src/main/java/".replace(".", fileSeparator));
File packageDir = new File(srcDir, generatedPackageName.replace(".", fileSeparator));
packageDir.mkdirs();

File sourceFile = new File(packageDir, generatedClassName + ".java");
Files.write(sourceFile.toPath(), sourceSb.toString().getBytes(StandardCharsets.UTF_8));

// Compile source file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(System.in, System.out, System.err, sourceFile.getPath());

// Load and instantiate compiled class
URL[] generatedClassUrls = {srcDir.toURI().toURL()};
ClassLoader parentClassLoader = compiler.getClass().getClassLoader();
try (URLClassLoader classLoader = URLClassLoader.newInstance(generatedClassUrls, parentClassLoader)) {
    Class<?> cls = Class.forName(generatedPackageName + "." + generatedClassName, true, classLoader);
    Object instance = cls.getDeclaredConstructor().newInstance();
    return instance;
}