How to compile sources at runtime

Before compiling sources at runtime you must create them: to do this follow this guide. If you need to compile the sources and load the generated classes at runtime it is suggested to use the ClassFactory, otherwise if you just need to compile the sources you can use the JavaMemoryCompiler as follow:

package source.compilation.test;


import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.classes.JavaMemoryCompiler;
import org.burningwave.core.classes.JavaMemoryCompiler.Compilation;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;
import org.burningwave.core.io.FileSystemItem;

import source.generation.test.SourceGenerationTester;

public class SourceCompilationTester {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
        ComponentContainer componentContainer = ComponentContainer.getInstance();
        JavaMemoryCompiler javaMemoryCompiler = componentContainer.getJavaMemoryCompiler();
        ProducerTask<Compilation.Result> compilationTask = javaMemoryCompiler.compile(
            Compilation.Config.forUnitSourceGenerator(
                SourceGenerationTester.generate()
            )
            .storeCompiledClassesTo(
                System.getProperty("user.home") + "/Desktop/classes"
            )
        );
        
        Compilation.Result compilationResult = compilationTask.join();
        
        System.out.println("\n\tAbsolute path of compiled file: " + 
            compilationResult.getClassPath()
            .findFirstInAllChildren(
                FileSystemItem.Criteria.forAllFileThat(FileSystemItem::isFile)
            ).getAbsolutePath() + "\n"
        );
    }
    
}

You can also load the generated class files:

package source.compilation.test;

import static org.burningwave.core.assembler.StaticComponentContainer.ClassLoaders;
import static org.burningwave.core.assembler.StaticComponentContainer.Methods;

import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.classes.JavaMemoryCompiler;
import org.burningwave.core.classes.JavaMemoryCompiler.Compilation;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;

import source.generation.test.SourceGenerationTester;

public class SourceCompilationTester {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
        ComponentContainer componentContainer = ComponentContainer.getInstance();
        JavaMemoryCompiler javaMemoryCompiler = componentContainer.getJavaMemoryCompiler();
        ProducerTask<Compilation.Result> compilationTask = javaMemoryCompiler.compile(
            Compilation.Config.forUnitSourceGenerator(
                SourceGenerationTester.generate()
            )
            .storeCompiledClassesTo(
                System.getProperty("user.home") + "/Desktop/classes"
            )
        );
        
        Compilation.Result compilattionResult = compilationTask.join();
        
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        ClassLoaders.addClassPaths(classLoader, compilattionResult.getDependencies());
        ClassLoaders.addClassPath(classLoader, compilattionResult.getClassPath().getAbsolutePath());
        Class<?> cls = classLoader.loadClass("source.generation.test.GeneratedClass");
        Methods.invokeStaticDirect(cls, "main", new Object[] {new String[] {"Hello", "world!"}});
    }
    
}

Flexible

It’s possible to search classes by every criteria that your imagination can make by using lambda expressions

Optimized

Scan engine is highly optimized using direct allocated ByteBuffers to avoid heap saturation

Open

Burningwave core is an advanced free and open source Java library