Which library/program can be used to generate Java-bytecode? [closed]

I know about BCEL, but this project seems to be dead, as it had no releases for two years. And the Java-world moves on. For example JDK 1.6 has a new class-file-format.

So what library can be used to create bytecode for the JVM. If no library, a program is ok too, if I can manipulate the generated code in detail, for example a bytecode-assembler.

Which software can you recommend? Is it easy too use? has good examples/tutorials?

EDIT: For all asking: Yes, the javac is fine. But for generating some classes at runtime, a path directly to bytecode would be cleaner.

Asked by: Agata244 | Posted: 28-01-2022

Answer 1



It is much faster than BCEL and supports generics and annotations. One point about its architecture: in order to ensure high performance ASM is built around a parser that throws events (in contrast to BCEL where the parser builds a data structure). This is somewhat similar to the difference between SAX and DOM parsers. It takes some practice to get used to this kind of thinking.

EDIT (Following McDowell's comment): Indeed visitors are heavily used in ASM, but it's more than plain visitors: the visited data structure is lazily built by the parser, so if you're not interested in certain parts of the classfile (for example, you want to know the names of the methods but you don't care about their body), you can return a null from the visitMethod() method. This will make the parser skip the method body sections thereby preventing the (expensive) construction of the net of objects fully describing the method.

Answered by: Miller832 | Posted: 01-03-2022

Answer 2

There is a fairly complete example of using ASM to generate byte code from a Java-like intermediate language in the implementation of CAL (a Haskell-like language for the JVM). If you download the sources at http://openquark.org/Open_Quark/Download.html then you can find the code in AsmJavaByteCodeGenerator.java, and the java model classes in the same folder. The code generated is basically what javac would do, minus debug annotations.

The CAL implementation originally used BCEL but switched to ASM because ASM was significantly faster (probably an order of magnitude), and just as significantly, ASM is thread safe, so that concurrent compilation is possible, which is needed by CAL.

Answered by: Daryl939 | Posted: 01-03-2022

Answer 3

http://serp.sourceforge.net/ is a great library for more abstraction when editing the bytecode.

Answered by: Brad305 | Posted: 01-03-2022

Answer 4

Javassist and cglib are two good bytecode engineering libraries. They are used extensively in the Java EE world for generating proxies of objects at runtime. Hibernate and Spring are two leading frameworks making use of these libraries.

Answered by: Kellan922 | Posted: 01-03-2022

Answer 5

There are technologies like asm and cglib but I recomend Javaassist because it is a very good library for that and you can find examples in tapestry5 framework.

Answered by: Ada947 | Posted: 01-03-2022

Answer 6

I think my favorite java bytecode creator is called javac and you can find it at www.sun.com

Answered by: Sienna600 | Posted: 01-03-2022

Answer 7

Why not use the Java compiler, javac? What's wrong with using it to generate JVM byte code?

[Seriously. What stops you from taking your source, making Java and compiling it?]

Answered by: Chester541 | Posted: 01-03-2022

Similar questions

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)