Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News SourceBuddy Compiles Dynamically Created Java Source Code

SourceBuddy Compiles Dynamically Created Java Source Code

Two months after the first commit in October 2022, Peter Verhas, senior architect at EPAM Systems, has released version 2.0.0 of SourceBuddy, a new utility that compiles dynamically created Java source code defined by a String or a file to a class file. SourceBuddy, requiring Java 17, is a simplified facade for the javac compiler, which delivers the same functionality.

Version 2.0.0 supports a combination of hidden and non-hidden classes during compilation and run-time. Furthermore, the API has been simplified, containing breaking changes such as changing the loadHidden() method to the hidden() method, hence the new major release. A complete overview of the changes per version is available in the releases documentation on GitHub.

SourceBuddy can be used after adding the following Maven dependency:


Alternatively the following Gradle dependency may be used:

implementation 'com.javax0.sourcebuddy:SourceBuddy:2.0.0'

To demonstrate SourceBuddy, consider the following example interface which will be used by the dynamically created code:


public interface PrintInterface {
	void print();

The simple API is able to compile one class at a time by using the static com.javax0.sourcebuddy.Compiler.compile() method. For example, to compile a new class implementing the previously mentioned PrintInterface:

String source = """

public class CustomClass implements PrintInterface {
    public void print() {
        System.out.println("Hello world!");
Class<?> clazz = Compiler.compile(source);
PrintInterface customClass = 
    (PrintInterface) clazz.getConstructor().newInstance();

The fluent API offers features to solve more complex problems such as the compilation of multiple files with the static method:;

Optionally, the binary name of the class may be specified, although SourceBuddy will already detect the name whenever possible:

.from("", source)

For multiple source files, the from() method may be called multiple times, or all the source files in a specific directory may be loaded at once:


Optionally, the hidden() method may be used to create a Hidden Class which can't be used directly by other classes, only through reflection by using the class object returned by SourceBuddy.

The compile() method generates the byte codes for the Java source files, but doesn't load them into memory yet.

final var byteCodes =
    .from("", source)

Optionally, the byte codes may be saved to the local drive:


Alternatively, the stream() method, which returns a stream of byte arrays, may be used to retrieve information such as the binary name:
    bytecode -> System.out.println(Compiler.getBinaryName(bytecode)));

The byteCodes.load() method loads the classes and converts the bytecode to Class objects:

final var loadedClasses = compiled.load();

Accessing a class is possible by casting to a superclass or an interface the class implements or by using the reflection API. For example, to access the CustomClass:

Class<?> customClass = loadedClasses.get("");

Alternatively, the newInstance() method may be used to create an instance of the class:

Object customClassInstance = loadedClasses.newInstance("");

The stream of classes may be used to retrieve more information about the classes:
    clazz -> System.out.println(clazz.getSimpleName()));

More information about SourceBuddy can be found in the detailed explanations inside the README file on GitHub.

About the Author

Rate this Article