116

Possible Duplicate:
Is there a java classfile / bytecode editor to edit instructions?

Java source code is compiled into bytecode, which is actually in the class file. Is it possible to view bytecode of a compiled class?

If it is possible, can it be edited?

Is there an eclipse plugin for that available?

5
  • 1
    Check out this question for java byte code editors. Commented Jul 23, 2010 at 7:00
  • 2
    What is the underlying problem you need to solve? Commented Jul 23, 2010 at 7:15
  • I am also interested in @Thorbjørn's question. @Abhishek What information are you seeking in the class file? or just out of curiosity? Commented Aug 3, 2010 at 2:11
  • 2
    I found a website which allows you to input java code and get the bytecode as the output. nodejs-instantbytecode.rhcloud.com Commented Feb 16, 2015 at 8:44
  • I had this need for creating a forked java process which could delegate to the class loader in this process without having to override it. If you implement your own classloader, you get to load the bytecode into a class. But, if you start with a class you can't convert it to bytecode for shipping to an external process. My need was to get it at runtime - not view/modify it for curiosity. Commented Mar 8, 2018 at 18:31

5 Answers 5

168

Yes. You can use the javap command that's included with the JDK to see the byte code of a class. For example:

javap -c com.mypackage.MyClass

There are several libraries and tools that help you to work with Java bytecode, for example ASM and Jasmin.

Sign up to request clarification or add additional context in comments.

3 Comments

By default private methods are not included. Use -p to include them as well.
For me only worked with the file name (extension seems to be optional though), but not with the fully qualified class name: javap -c -p MyClass.class
javap -c com.mypackage.MyClass will print the code on the terminal(stdout). I prefer javap -c -p com.mypackage.MyClass >> com.mypackage.MyClass.txt to see the code in a file.
28

The JDK comes with javap which is a tool to disassemble the byte code inside a class file. Editing on byte code level is possible. Have a look at BCEL, a java library designed to read, manipulate and write class files.

A list of tool and libraries to edit byte code can be found on java-net. For example JBE, a Java Byte Code editor that even comes with a GUI.

Comments

19

To view the bytecodes

Forget javap! The best plugin I have ever used is the "ASM - Bytecode Outline plugin for Eclipse"

http://asm.ow2.org/eclipse/index.html

It is from ASM (a bytecode manipulation framework).

It shows the bytecodes (that you asked for), stack elements (jvm style), and how to generate the same result (to produce the same bytecodes) using the asm framework methods.

Better still is the fact that it does so while you have the source code selected. You don't have to find the .class file in the bin directory to inspect it's bytecode.

To edit them

Using code:

  • ASM: Visitors based, very, very fast.
  • BCEL: Loads the bytecode as an in memory description of the class file.
  • Javassit: the easiest one to use, allows you to do pattern matching and expression replacement.

By hand: JBE

3 Comments

+1 I've used BCEL and ASM, ASM is neat. It has a visitor to decompile into the console already there. I mean, it's more capable that javap. The Eclipse plugin rocks. It's very fast!
Failed installation on Mars 2 (4.5.2) =( It attempted some compatibility fixes but ultimately it didn't work
Eclipse? Try Intellij it also as an ASM plugin
5

To my experience, jclasslib is one of the best bytecode viewers.

As for editors, there are two types: bytecode manipulation libraries, and editors with GUIs. This question has been asked few times on SO, you could check the answers and the links that were provided.

Just be careful that editing bytecode in not as straightforward as you think. The JVMS imposes many restrictions on how class files should be, and there is a great chance that one of your edit will violate one of them.

Check these other questions:

Editing a .class file directly, playing around with opcodes

Is it possible to view bytecode of Class file?

Programming in Java bytecode

Comments

0

Try use - dirtyJOE - Java Overall Editor is a complex editor and viewer for compiled java binaries (.class files).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.