Is it possible to programmatically compile java source code in memory only?

To start, look at the JavaCompiler API. Basically: Create the Java class in a string. Put the string into class that extends SimpleJavaFileObject. Compile using a JavaCompiler instance. Finally, call the methods the new class. Here is an example that works with JDK6+: import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.util.Arrays; … Read more

How to set classpath when I use javax.tools.JavaCompiler compile the source?

The javax.tools.JavaCompiler#getTask() method takes an options parameter that allows to set compiler options. The following message describes an easy way to set them in order to access the calling program’s classpath: You need to configure the standard java file manager to know about the jar files(s) – you use the compiler options argument to do … Read more

Compile code fully in memory with javax.tools.JavaCompiler [duplicate]

I’ve run the above code in Mac OS Java 7. None of them works. So i wrote one https://github.com/trung/InMemoryJavaCompiler StringBuilder source = new StringBuilder() .append(“package org.mdkt;\n”) .append(“public class HelloClass {\n”) .append(” public String hello() { return \”hello\”; }”) .append(“}”); Class<?> helloClass = InMemoryJavaCompiler.compile(“org.mdkt.HelloClass”, source.toString());