Location>code7788 >text

The artifact of dynamically compiling Java is released by Liquor v1.3.10

Popularity:985 ℃/2025-02-17 09:16:25

Liquor is an open source lightweight Java dynamic compiler (zero dependency, 40KB) that can compile Java string code snippets, classes, methods, etc. at runtime.

  • Source code address:/noear/liquor

Basic compilation features:

  • Can be compiled in a single class
  • Multiple classes can be compiled at the same time
  • Can be compiled incrementally

Value-added features:

  • java expression engine
  • java scripting engine (supports java8 to java23 syntax)

This update

  • Add toScripts:eval(String, Map)Convenient method
  • optimizationLiquorEvaluatorCompiled scripts allow display of thrown exceptions

Feature Demonstration

  • Dynamic compiler application
//It can be reused (no, constantly new creation)
 DynamicCompiler compiler = new DynamicCompiler();

 String className = "HelloWorld";
 String classCode = "import ;\n\n"+
         "public class HelloWorld { " +
         " public static void helloWorld() { " +
         " (\"Hello, world!"); " +
         " } " +
         "}";

 //Add source code (multiple) and build
 (className, classCode);
 ();

 Class<?> clazz = ().loadClass(className);
 ("helloWorld").invoke(null);
  • Expression application
Map<String, Object> ctx = new HashMap<>();
("b", 3);
("a", 22);

("b + a", ctx); //=>25
  • Script application
("(\"hello word\");");