Class RunningJVM

java.lang.Object
uno.anahata.ai.tools.spi.RunningJVM

public class RunningJVM extends Object
Provides tools for the AI model to compile and execute Java source code within the application's running JVM.

This enables a powerful "hot-reload" workflow where the model can write, test, and run code dynamically. It uses an in-memory file manager and a custom classloader to ensure that newly compiled classes take precedence.

Author:
anahata
  • Field Details

    • chatTemp

      public static Map chatTemp
      A thread-safe map that can be used by the model to persist state across multiple dynamic code executions.
    • defaultCompilerClasspath

      public static String defaultCompilerClasspath
      The default classpath used by the compiler.
  • Constructor Details

    • RunningJVM

      public RunningJVM()
  • Method Details

    • initDefaultCompilerClasspath

      public static String initDefaultCompilerClasspath()
      Initializes the default compiler classpath from the system property.
      Returns:
      The system classpath.
    • getDefaultCompilerClasspath

      public static String getDefaultCompilerClasspath()
      Gets the current default compiler classpath.
      Returns:
      The classpath string.
    • setDefaultCompilerClasspath

      public static void setDefaultCompilerClasspath(String defaultCompilerClasspath)
      Sets a new default compiler classpath.
      Parameters:
      defaultCompilerClasspath - The new classpath string.
    • getPrettyPrintedDefaultCompilerClasspath

      public static String getPrettyPrintedDefaultCompilerClasspath()
      Returns a pretty-printed, tree-like representation of the default classpath.
      Returns:
      The formatted classpath string.
    • compileJava

      public static Class compileJava(String sourceCode, String className, String extraClassPath, String[] compilerOptions) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
      Compiles Java source code in memory and returns the resulting Class object.
      Parameters:
      sourceCode - The Java source code to compile.
      className - The fully qualified name of the class.
      extraClassPath - Additional classpath entries to include.
      compilerOptions - Additional options for the Java compiler.
      Returns:
      The compiled Class object.
      Throws:
      ClassNotFoundException - if the class cannot be found after compilation.
      NoSuchMethodException - if a required constructor is missing.
      IllegalAccessException - if the class or constructor is not accessible.
      InvocationTargetException - if the constructor throws an exception.
    • compileAndExecuteJava

      public static Object compileAndExecuteJava(String sourceCode, String extraClassPath, String[] compilerOptions) throws Exception
      Compiles and executes a Java class named Anahata that implements Callable.
      Parameters:
      sourceCode - The Java source code.
      extraClassPath - Additional classpath entries.
      compilerOptions - Additional compiler options.
      Returns:
      The result of the call() method, ensured to be JSON-serializable.
      Throws:
      Exception - if compilation or execution fails.