2

When searching the web it is easy to find this way to print out Java bytecode:

http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/

What I could not find is if there is a standard representation of Java bytecode as a string and if there is a tool to compile string representation of Java bytecode into binary bytecode.

In .NET the equivalent tool is ilasm.exe. Is there something like this for Java and is it standard?

5 Answers 5

3

There are tools to do this but they aren't part of the standard. You have two options:

  • You can use the Java(tm) Bytecode Assembler or Jasmin. They take a text file as input and compile that to a class file.
  • You can use a library like ASM to build bytecode using Java code. This the usual approach when you read existing bytecode and want to transform it.
Sign up to request clarification or add additional context in comments.

2 Comments

The Java Bytecode Assembler page still exists, unfortunately the tar.gz of the assembler has vanished :o(
I eventually found another version.
1

Take a look at for example ASM or Jasmin; those are Java bytecode assemblers.

The JDK does not contain a bytecode assembler as one of its standard tools.

2 Comments

My understanding is that ASM manipulates the code on binary leve. Jasmin seems to be what I asked for but I am curious if it standard or just a language chosen by the developers.
The JVM specification is probably closest to what you can get for standard names of opcodes.
1

In the past I've used Jasmin.

Comments

1

What I could not find is if there is a standard representation of Java bytecode as a string ...

There isn't one.

However, there are 3rd party tools for creating classfiles in a variety of ways. Bytecode asemblers like ASM or Jasmin are one approach, and another approach is to generate the bytecodes programatically using something like BCEL.

(The standard javap utility can be told to output formatted bytecodes, but the format is not standardized, and there's no standard utility that goes in the other direction.)

Comments

1

If you're just trying to view the byte code of a .class file, javap is your friend.

There isn't really a "standard" way to convert Java bytecode to assembly -- since that's generally done only at runtime by the JIT -- but gcj does something along those lines.

7 Comments

I was not looking for a way to convert Java bytecode to assembly but a way to convert string representation of Java bytecode to binary bytecode.
I don't believe there's a standard string representation of Java bytecode, which more or less implies that that's not possible.
Yeah it seems like the tools in other answers invented their own syntax for java bytecode to make it possible.
A better question would be "why would you ever want to do something like this."
This is in fact quite interesting question. Some years ago when I was in the university I had compiler construction course. I implemented a .NET compiler for a simple language but my head hurt enough learning about compilers and I just couldn't learn the Reflection.Emit API in .NET that emit binary IL. I just generated a string and called into ilasm.exe to compile it. This proved easier to debug as well because I displayed the generated IL in a textbox. Yesterday a guy asked me about implementing the same project for Java bytecode and I was surprised that I could not find similar tool.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.