DEV Community

Cover image for How to generate and compile sources at runtime in Java

How to generate and compile sources at runtime in Java

Burningwave is a set consisting of a Java frameworks building library and other applications and frameworks based on it
Updated on ・2 min read

With ClassFactory it is possible to generate classes at runtime, but how we can do if we only need to compile sources without loading them or just generate sources?

Here they come to our aid the source code generator components and the JavaMemoryCompiler of Burningwave Core. With source code generators we can generate source code and store it on the drive or compile it via the JavaMemoryCompiler.

To use all these components you should simply add the following to your projects dependencies:

Now we are going to generate and store the following class:

... With the following source code generators:

And now let's try to compile the sources and store the compiled files with the JavaMemoryCompiler:

And now let's try to load the compiled file:


In this tutorial we learned how to generate and compile sources and how to load the generated class files, you can do a lot of things which are not mentioned here like, for example, compile sources that references classes located outside the runtime class paths and if you are curious or need further help you can request it at the official forum.

Discussion (0)