Stilgar
Stilgar

Reputation: 23551

How to compile Java bytecode string?

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?

Upvotes: 2

Views: 3225

Answers (5)

Aaron Digulla
Aaron Digulla

Reputation: 328556

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.

Upvotes: 3

Louis Wasserman
Louis Wasserman

Reputation: 198023

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718718

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.)

Upvotes: 1

Luca Martini
Luca Martini

Reputation: 1474

In the past I've used Jasmin.

Upvotes: 1

Jesper
Jesper

Reputation: 206786

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.

Upvotes: 1

Related Questions