Class JavaShell

java.lang.Object
org.apache.groovy.util.JavaShell

public class JavaShell extends Object
A shell for compiling or running pure Java code in-memory using the platform's JavaCompiler. Compiled bytes are kept in a backing class loader (see getClassLoader()) and can optionally be written to disk via compileAllTo(String, Iterable, String, Path).

The className argument supplied to run, compile, compileAll and compileAllTo must be the fully-qualified binary name and match any package declaration in src: if the source begins with package com.example; and declares class Foo, pass "com.example.Foo", not "Foo". A mismatch produces a standard javac diagnostic ("class X is public, should be declared in a file named X.java"). Source with no package declaration takes a simple name (e.g. "Foo"); compileAllTo writes such a class directly under outputDir as Foo.class, while packaged classes land under the matching subdirectory (e.g. <outputDir>/com/example/Foo.class).

The Iterable<String> compiler-options parameter accepted by run, compile, compileAll and compileAllTo uses the same flags as the javac command line, with one token per element (so "-source" and "17" are two entries, not a single "-source 17" string). Examples:

  • List.of("--release", "17") — target a specific Java release
  • List.of("-source", "17", "-target", "17") — separate source/target levels
  • List.of("-parameters") — retain formal parameter names
  • List.of("-g") — emit debug information
  • List.of("-Xlint:all", "-Werror") — enable all warnings and treat them as errors
  • List.of("-proc:none") — disable annotation processing
  • List.of("-classpath", extraJars) — extend the compile-time class path
The -d flag has no effect: in-memory output is always captured (and is additionally written to disk by compileAllTo). See the javac documentation for the complete list of supported flags.