Wednesday, May 17, 2023

How to decompile class file in Java and Eclipse - Javap command example

The ability to decompile a Java class file is quite helpful for any Java developer who wants to look into the source of any open source or propriety library used in a project. Though I always prefer to attach sources in Eclipse of most common libraries like JDK it’s not always possible with an increasing number of dependencies. Java decompiler (a program that can decompile Java class files to produce source files) is very helpful in such a situation. By using Java decompiler you can easily check out the source of any .class file. Thanks to Eclipse IDE and the increasing number of free plugins available for Java developers, You can have powerful Java decompile in your armory.

Earlier I used to use JadEclipse an Eclipse plugin that works well with JAD decompiler but knowing about the JAD decompiler is not supporting Java 1.5 source, I was in hunt of another suitable and useful Eclipse plugin which can decompile .class file. 

My search ends with JD-Eclipse , this is another free Eclipse plugin for non-commercial and personal use which helps you to get the source from class files. JD-Eclipse is easy to install and its site has a detailed step by step guide on how to install JD-Eclipse plug-in. If you are already familiar with Eclipse plugin installation then it just a cakewalk.


How to decompile class file in Java and Eclipse - Javap command example




How to decompile Class file in Eclipse IDE

how to decompile class file in Java and Eclipse with javap command exampleOnce you have JD-Eclipse installed and running, you probably need to restart Eclipse once you installed the plug-in. You are ready to decompile Java class file from Eclipse. In order to decompile class file, just open any of your Java projects and go to Maven dependencies of libraries to see the jar files included in your project. 

Just expand any jar file for which you don't have the source attached in Eclipse and click on the .class file. Now you can see the source code for that file in Java editor, simple and elegant isn't it. 

Though I highly recommend attaching source code for JAR in Eclipse, at least for frequently used libraries like JDK or Spring as the quality of decompiled code and the original code is different. The original code gives better visibility and also shows comment while decompiled code is just the bare minimum version.



How to decompile class file in Java – javap command example

Even with powerful Eclipse IDE and plugin, we may sometimes need to work on command prompt especially while working in Linux development servers and its not convenient to switch back and forth for quick look on .class file or get some information from compiled form of source. 

Thanks to javap command you can decompile class file on the fly in command prompt. javap is standard binary which comes with JDK installation and resides in JAVA_HOME/bin directory. javap is similar to javac (java compiler) and work directly with .class file. 

In order to use javap command you must have JAVA_HOME in your system path. you can verify this by typing "javap" in command prompt and if it doesn't complain and instead show some output like below than you are good to go. If it doesn't recognize the command means you need to set path, check how to set path in java more detailed steps.

abc@localhost:~/java javap
No classes were specified on the command line.  Try -help.

Now let's see what javap command offers us. We have a simple Java class with one field and one method.

abc@localhost:~/java cat Hello.java

public class Hello{
        private String version="1.2";
        public static void main(String args[]){
                System.out.println("Running Hello");
        }
}

abc@localhost:~/java javap Hello
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
    public Hello();
    public static void main(java.lang.String[]);
}

So it looks javap only provides information related to a method in the .class file. It also states the constructor even default constructor added by Java compiler. javap can provide more information depending upon its command line option like -private will show all private members. here is an example of running javap command with the -private option:

abc@localhost:~/java javap -private Hello
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
    private java.lang.String version;
    public Hello();
    public static void main(java.lang.String[]);
}


How to see bytecode from .class file

javap  command can also show  bytecodes form compiled class files. Just run javap with -c option and it will print bytecodes of  class file as shown in below example:

abc@localhost:~/java javap -c Hello
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   aload_0
   5:   ldc     #2; //String 1.2
   7:   putfield        #3; //Field version:Ljava/lang/String;
   10:  return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic       #4; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #5; //String Running Hello
   5:   invokevirtual   #6; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

}

That's all on how to decompile class files in Java and Eclipse IDE. JD-Eclipse is easy to use the eclipse plugin and has detailed installation steps documented. if you are running on JDK lower than Java 5 than You can still use a famous JAD decompiler and JADEclipse plug-in. Apart from these are much more which I haven't tried. Just go to the Eclipse marketplace and you will see a lot of those.


Relate Eclipse shortcuts and tutorials from Javarevisited Blog

5 comments :

Unknown said...

JD-Eclipse is certainly useful, but it didn't go quite far enough. You need the decompiled code to be aligned with its original line numbers, so you can set breakpoints in the decompiled code. That's where http://mchr3k.github.com/jdeclipse-realign/ comes in.

Unknown said...

That is true David. I have provided a step by step instructions here to install JD-Eclipse, hopefully that will be helpful to some of our fellow programmers.

http://www.coolwebdeveloper.com/2013/04/how-to-add-jd-eclipse-plugin-java-decompiler-to-eclipse-juno/

Anonymous said...

What is the best ways to decompile Java class files from command prompt? Examples given here doesn't show how to get full decompiled code, rather than byte code or just method and fields name. Precisely, I have a Java class file, which doesn't use any library and I want to see the code. I don't use IDE for such purpose, so any plain old command will help. Thanks

Garry said...

There are lots of free and open source Java decompilers are out there, you can use any of them to decompile java class files from command prompt. Here is a list of some popular Java class file decompilers :

Procyon (open-source and actively developed)
Krakatau (open-source and actively developed)
CFR
JAD
DJ Java Decompiler
Mocha
JAD Decompiler
Soot
Jdec

You can even use online class file decompilers like http://showmycode.com to upload your class file and see there source code on browser itself, quite convenient if you can upload your class file via internet.

Anonymous said...

javap -c is the real command as it shows the bytecode instructions e.g.invokedynamic, invokespecial, invokevirtual etc, but you can also use javap -c -v to see data from constant pool, where these method invocation links.

Post a Comment