generation of java bytecode using JCodeModel

403 views Asked by At

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

1

There are 1 answers

0
rsutormin On

You can consider one of several java byte code generators: Any Java Bytecode Generation Guide?

But if you prefer to deal with JCodeModel lib you have an option to keep intermediate java code in memory as temporary stage. Here is sequence of hints:

(1) Here is an example how to get java source text in memory: Compile dynamically generated class at runtime w/o writing to File

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

(3) Finally to create jar-file from byte code stored in memory just do thing similar to: How to use JarOutputStream to create a JAR file?

PS: Last stage could be done as stream writing directly to database (BLOB field or so).