Class JavaShell
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 releaseList.of("-source", "17", "-target", "17")— separate source/target levelsList.of("-parameters")— retain formal parameter namesList.of("-g")— emit debug informationList.of("-Xlint:all", "-Werror")— enable all warnings and treat them as errorsList.of("-proc:none")— disable annotation processingList.of("-classpath", extraJars)— extend the compile-time class path
-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.-
Constructor Summary
ConstructorsConstructorDescriptionInitializes a newly createdJavaShellobjectJavaShell(ClassLoader parentClassLoader) Initializes a newly createdJavaShellobject -
Method Summary
Modifier and TypeMethodDescriptionClass<?>Compile and return the main classClass<?>Compile and return the main classcompileAll(String className, Iterable<String> options, String src) Compile and return all classescompileAll(String className, String src) Compile and return all classesCompilesrcand write each resulting class file beneathoutputDiras a standard package directory tree (e.g.compileAllTo(String className, String src, Path outputDir) Convenience overload ofcompileAllTo(String, Iterable, String, Path)that compiles with no extra compiler options.org.apache.groovy.util.JavaShell.JavaShellClassLoaderWhen and only whencompile(String, String)orcompileAll(String, String)is invoked, returned class loader will reference the compiled classes.voidRun main methodvoidRun main method
-
Constructor Details
-
JavaShell
public JavaShell()Initializes a newly createdJavaShellobject -
JavaShell
Initializes a newly createdJavaShellobject- Parameters:
parentClassLoader- the parent class loader for delegation
-
-
Method Details
-
run
public void run(String className, Iterable<String> options, String src, String... args) throws Throwable Run main method- Parameters:
className- the main class nameoptions- compiler optionssrc- the source codeargs- arguments for main method- Throws:
Throwable
-
run
Run main method- Parameters:
className- the main class namesrc- the source codeargs- arguments for main method- Throws:
Throwable
-
compile
public Class<?> compile(String className, Iterable<String> options, String src) throws IOException, ClassNotFoundException Compile and return the main class- Parameters:
className- the main class nameoptions- compiler optionssrc- the source code- Returns:
- the main class
- Throws:
IOExceptionClassNotFoundException
-
compile
Compile and return the main class- Parameters:
className- the main class namesrc- the source code- Returns:
- the main class
- Throws:
IOExceptionClassNotFoundException
-
compileAll
public Map<String,Class<?>> compileAll(String className, Iterable<String> options, String src) throws IOException, ClassNotFoundException Compile and return all classes- Parameters:
className- the main class nameoptions- compiler optionssrc- the source code- Returns:
- all classes
- Throws:
IOExceptionClassNotFoundException
-
compileAll
public Map<String,Class<?>> compileAll(String className, String src) throws IOException, ClassNotFoundException Compile and return all classes- Parameters:
className- the main class namesrc- the source code- Returns:
- all classes
- Throws:
IOExceptionClassNotFoundException
-
compileAllTo
@Incubating public Map<String,Path> compileAllTo(String className, Iterable<String> options, String src, Path outputDir) throws IOException Compilesrcand write each resulting class file beneathoutputDiras a standard package directory tree (e.g.com.example.Foo$Barbecomes<outputDir>/com/example/Foo$Bar.class). Intermediate directories are created as needed; existing class files at those locations are overwritten. The compiled classes also remain available throughgetClassLoader().- Parameters:
className- the main class name (binary name, e.g.com.example.Foo)options- compiler options; see the class-level documentation for the expected formatsrc- the source codeoutputDir- root directory under which class files are written; created if absent- Returns:
- a map from binary class name to the
Pathof the written.classfile, iterating in the order the compiler emitted them (stable across invocations for the same source, but JDK-dependent — inner and auxiliary classes commonly appear before the enclosing class) - Throws:
IOException- if writing a class file failsJavaShellCompilationException- if the source fails to compile- Since:
- 6.0.0
-
compileAllTo
@Incubating public Map<String,Path> compileAllTo(String className, String src, Path outputDir) throws IOException Convenience overload ofcompileAllTo(String, Iterable, String, Path)that compiles with no extra compiler options.- Parameters:
className- the main class name (binary name)src- the source codeoutputDir- root directory under which class files are written; created if absent- Returns:
- a map from binary class name to the
Pathof the written.classfile - Throws:
IOException- if writing a class file failsJavaShellCompilationException- if the source fails to compile- Since:
- 6.0.0
-
getClassLoader
public org.apache.groovy.util.JavaShell.JavaShellClassLoader getClassLoader()When and only whencompile(String, String)orcompileAll(String, String)is invoked, returned class loader will reference the compiled classes.
-