Generating java bytecode using JCodeModel

I created a JCodeModel that contains all the classes that I want to generate. Thing is, I want to generate bytecode (.class files) and jar, but not sources. Is there an elegant way to do this without generating .java files and later compiling thme to .class and jar files?

+3


source to share


1 answer


You may want to consider one of several java byte code generators: Guide to Java Byte Code Generation?

But if you prefer to work with the JCodeModel lib, you have the option to keep the intermediate Java code in memory as temporary. Here is the sequence of prompts:

(1) Here is an example of how to get the java source in memory: Compile the dynamically generated class at runtime without writing to a file



(2) Then you can use a similar thing to keep the compiled bytecode in memory again: https://github.com/trung/InMemoryJavaCompiler/blob/master/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler. java

(3) Finally, to create a jar file from bytecode stored in memory, just do the following thing: How to use JarOutputStream to create a JAR file?

PS: The last step can be done as a streaming write directly to the database (BLOB field or so).

0


source







All Articles