The purpose of this project is to provide a simple API for in-memory compilation of Java code. While the JDK provides a full-blown API for this, in many cases a much simpler API is sufficient.
This project is based on abandoned code by @trung and a reduced version of language utils from VRL.
Note: This project needs the full JDK as runtime-dependency to work properly (the JRE does not contain the JavaCompiler API).
Compiling Java classes is as simple as this:
// compile code
CompilationResult result = JCompiler.newInstance().
compile(
"public class MyClass {\n"+
" public static String hello() {\n"+
" return \"hello\";\n"+
" }\n"+
"}"
).checkNoErrors();
// load class and invoke static method
Class<?> myClass = result.loadClasses().get("MyClass");
String msg = (String)myClass.getMethod("hello").invoke(null);
- JDK >= 11
- Internet Connection (other dependencies will be downloaded automatically)
- Optional: IDE with Gradle support
To build the project from an IDE do the following:
- open the Gradle project
- call the
assemble
Gradle task to build the project
Building the project from the command line is also possible.
Navigate to the project folder and call the assemble
Gradle
task to build the project.
cd Path/To/JCompiler
./gradlew assemble
cd Path\To\JCompiler
gradlew assemble
cd Path\To\JCompiler
.\gradlew assemble