views:

386

answers:

6

Often I am stuck with a java class file with no source and I am trying to understand the problem I have at hand.

Note a decompiler is useful but not sufficient in all situation...

I have two question

  1. What tools are available to view java byte code (preferably available from the linux command line )
  2. What are good references to get familiar with java byte code syntax
+5  A: 

Rather than looking directly at the Java bytecode, which will require familiarity with the Java virtual machine and its operations, one could try to use a Java decompiling utility. A decompiler will attempt to create a java source file from the specified class file.

The How do I “decompile” Java class files? is a related question which would be informative for finding out how to decompile Java class files.

That said, one could use the javap command which is part of the JDK in order to disassemble Java class files. The output of javap will be the Java bytecode contained in the class files. But do be warned that the bytecode does not resemble the Java source code at all.

The definite source for learning about the Java bytecode and the Java Virtual Machine itself would be The Java Virtual Machine Specification, Second Edition. In particular, Chapter 6: The Java Virtual Machine Instruction Set has an index of all the bytecode instructions.

coobird
see my comment regarding decompiler,
hhafez
@hhafez: I guess you're question "Understanding Java Byte Code" is answered, is it not?
Ryan Fernandes
+1  A: 

If you have a class and no source code, but you have a bug, you can do one of two basic things:

  1. Decompile, fix the bug and recreate the jar file. I have done this before, but sysadmins are leery about putting that into production.
  2. Write unit tests for the class, determine what causes the bug, report the bug with the unit tests and wait for it to be fixed.

    (2) is generally the one that sysadmins, in my experience, prefer.

    If you go with (2) then, in the meantime, since you know what causes the bug, you can either not allow that input to go to the class, to prevent a problem, or be prepared to properly handle it when the error happens.

    You can also use AspectJ to inject code into the problem class and change the behavior of the method without actually recompiling. I think this may be the preferable option, as you can change it for all code that may call the function, without worrying about teaching everyone about the problem.

    If you learn to read the bytecode instructions, what will you do to solve the problem?

James Black
1. I didn't know you could use AspectJ to insert code in .class, I thought it worked on the java, but I guess I am wrong2. what will I do when I figure out the problem? Many times finding out what the problem really is half way there to solving your problem
hhafez
@hhafez - You can do runtime weaving of aspects into classes, but, you may violate the license when doing that, so just be a bit wary. Unit tests may be your best bet, but if the code isn't testable then decompiling becomes an option, but it is a slow process, IMO.
James Black
A: 

I have two question

1) What tools are available to view java byte code (preferably available from the linux command line )

The javap tool (with the -c option) will disassemble a bytecode file. It runs from the command line, and is supplied as part of the Java SDK.

2) What are good references to get familiar with java byte code syntax

The javap tool uses the same syntax as is used in the JVM specification, and the JVM spec is naturally the definitive source. I also spotted "Inside the Java Virtual Machine" by Bill Venners. I've never read it, and it looks like it might be out of print.

The actual (textual) syntax is simple and self explanatory ... assuming that you have a reference that explains what the bytecodes do, and that you are moderately familiar with reading code at this level. But it is likely to be easier to read the output of a decompiler, even if the bytecodes has been fed through an obfuscator.

Stephen C
+3  A: 

To view bytecode instruction of class files, use the javap -v command, the same way as if you run a java program, specifying classpath (if necessary) and the class name.

Example:

javap -v com.company.package.MainClass

About the bytecode instruction set, Instruction Set Summary

yuku
+1  A: 

Fernflower is an analytical decompiler, so it will decompile classes to a readable java code instead of bytecodes. It's much more usefull when you want to understand how code works.

tulskiy
A: 

You might find the Eclipse Byte Code Outline plugin useful:

http://andrei.gmxhome.de/bytecode/index.html

I have not used it myself - just seen it mentioned in passing.

Thorbjørn Ravn Andersen