Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Java Byte Code

Tags:

java

bytecode

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
like image 656
hhafez Avatar asked Oct 12 '09 01:10

hhafez


4 Answers

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.

like image 80
coobird Avatar answered Oct 21 '22 16:10

coobird


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

like image 23
Randy Sugianto 'Yuku' Avatar answered Oct 21 '22 16:10

Randy Sugianto 'Yuku'


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.

like image 30
Denis Tulskiy Avatar answered Oct 21 '22 14:10

Denis Tulskiy


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?

like image 34
James Black Avatar answered Oct 21 '22 15:10

James Black