This library lets you feed plain Java source as aString, compile it in-memory and immediately load the resulting Class<?> - perfect for hot-swapping logic while the JVM is still running.
<!-- Maven -->
<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
<version>Look up the most recent version on Maven Central.</version>
</dependency>/* Gradle (Kotlin DSL) */
implementation("net.openhft:compiler:Look up the most recent version on Maven Central.")importnet.openhft.compiler.CompilerUtils;
StringclassName = "mypackage.MyDynamicClass";
Stringsrc = """ package mypackage; public class MyDynamicClass implements Runnable { public void run() { System.out.println("Hello World"); } }""";
Class<?> clazz = CompilerUtils.CACHED_COMPILER.loadFromJava(className, src);
((Runnable) clazz.getDeclaredConstructor().newInstance()).run();Requires a full JDK (8, 11, 17 or 21 LTS), not a slim JRE.
On Java 11 + supply these flags (copy-paste safe):
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-opens=jdk.compiler/com.sun.tools.javac=ALL-UNNAMEDSpring-Boot / fat-JAR users
unpack Chronicle jars:
bootJar { requiresUnpack("*/chronicle-.jar") }
| Feature | Benefit |
|---|---|
In-memory compile & load | Hot-swap code without restarting JVM |
CachedCompiler | Skips recompilation of unchanged source |
Debug Mode | Writes |
Custom ClassLoader support | Isolate plugins or enable class unloading |
Nested-class handling | Build helper hierarchy in a single call |
Hot-swappable strategy interface for trading engines
Rule-engine: compile business rules implementing
RuleSupports validator hook to vet user code
Replace reflection: generate POJO accessors, 10 x faster
Off-heap accessors with Chronicle Bytes / Map
Compile on a background thread at start-up; then swap instances.
Re-use class names or child classloaders to control Metaspace.
Use
CompilerUtils.DEBUGGING = trueduring dev; remember to prune artefacts.
SLF4J categories:
net.openhft.compiler(INFO), compilation errors at ERROR.Micrometer timers/counters:
compiler.compiles,compiler.failures.
Project requirements outline functional, non-functional, and compliance obligations.
Release notes provide the tagged release history.
`ToolProvider.getSystemJavaCompiler() == null`
You are running on a JRE; use a JDK.
`ClassNotFoundException: com.sun.tools.javac.api.JavacTool`
tools.jar is required on JDK ⇐ 8. Newer JDKs need the
--add-exportsand--add-opensflags.Classes never unload
Generate with a unique
ClassLoaderper version so classes can unload; each loader uses Metaspace.Illegal-reflective-access warning
Add the
--add-opens&--add-exportsflags shown above.
GitHub Actions workflow runs
mvn verify, unit & race-condition tests.Code-coverage report published to SonarCloud badge above.