Home > Java > JavaBase > body text

How to decompile java

Release: 2019-12-07 15:06:07
Original
8848 people have browsed it

How to decompile java

The process of decompilation is exactly the opposite of compilation, which is to restore the compiled programming language to its uncompiled state, that is, to find out the source code of the programming language. It is to convert the language that the machine can understand into the language that the programmer can understand. Decompilation in Java language generally refers to converting class files into java files.

Commonly used Java decompilation tools

This article mainly introduces four Java decompilation tools: javap, jad and cfr as well as the visual decompilation tool JD-GUI

JAVAP

javap is a tool that comes with jdk. It can decompile the code and view the bytecode generated by the java compiler. The biggest difference between javap and the other two decompilation tools is that the files it generates are not java files, and they are not as easy to understand as the codes generated by the other two tools. Take a simple piece of code as an example. If we want to analyze how switch in Java 7 supports String, we first have the following source code that can be compiled and passed:

public class switchDemoString {
    public static void main(String[] args) {
        String str = "world";
        switch (str) {
            case "hello":
                System.out.println("hello");
                break;
            case "world":
                System.out.println("world");
                break;
            default:
                break;
        }
    }
}
Copy after login

Execute the following two commands:

javac Decompilation.java
javap -c Decompilation.class
Copy after login

The generated code is as follows:

Compiled from "Decompilation.java"
public class Decompilation {
  public Decompilation();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #16                 // String world
       2: astore_1
       3: aload_1
       4: dup
       5: astore_2
       6: invokevirtual #18                 // Method java/lang/String.hashCode:()I
       9: lookupswitch  { // 2
              99162322: 36
             113318802: 48
               default: 82
          }
      36: aload_2
      37: ldc           #24                 // String hello
      39: invokevirtual #26                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
      42: ifne          60
      45: goto          82
      48: aload_2
      49: ldc           #16                 // String world
      51: invokevirtual #26                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
      54: ifne          71
      57: goto          82
      60: getstatic     #30                 // Field java/lang/System.out:Ljava/io/PrintStream;
      63: ldc           #24                 // String hello
      65: invokevirtual #36                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      68: goto          82
      71: getstatic     #30                 // Field java/lang/System.out:Ljava/io/PrintStream;
      74: ldc           #16                 // String world
      76: invokevirtual #36                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      79: goto          82
      82: return
}
Copy after login

javap does not decompile the bytecode into a java file, but generates a bytecode that we can understand. In fact, the files generated by javap are still bytecodes, but programmers can understand them a little more. If you have some knowledge of bytecode, you can still understand the above code. In fact, it is to convert String into hashcode and then compare.

JAD

JAD is a relatively good decompilation tool. As long as you download an execution tool, you can decompile class files. Still the above source code, the content after decompilation using jad is as follows:

Command: jad.exe Decompilation.class will generate a Decompilation.jad file

The results of JAD decompilation are as follows:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Decompilation.java

package com.yveshe;

import java.io.PrintStream;

public class Decompilation
{

    public Decompilation()
    {
    }

    public static void main(String args[])
    {
        String str = "world";
        String s;
        switch((s = str).hashCode())
        {
        default:
            break;

        case 99162322: 
            if(s.equals("hello"))
                System.out.println("hello");
            break;

        case 113318802: 
            if(s.equals("world"))
                System.out.println("world");
            break;
        }
    }
}
Copy after login

Look at the above code. Isn’t this the standard java source code? This clearly shows that the original string switch is implemented through the equals() and hashCode() methods.

CFR

JAD is very useful, but unfortunately it has not been updated for a long time, so we can only use a new tool to replace it. CFR is a good one choice, compared to JAD, his syntax may be slightly more complex, but fortunately he can use .

CFR will decompile modern Java features – Java 8 lambdas (Java in Java and earlier versions beta 103), Java 7 String has been decompiled, but CFR is completely written in Java 6.

It is recommended that you manually compile and generate the Decompilation.class file through the javac Decompilation.java command, and then test it.

The successful decompilation result is as follows:

/*
 * Decompiled with CFR 0_125.
 */
package com.yveshe;

import java.io.PrintStream;

public class Decompilation {
    public static void main(String[] args) {
        String str;
        String s = str = "world";
        switch (s.hashCode()) {
            default: {
                break;
            }
            case 99162322: {
                if (!s.equals("hello")) break;
                System.out.println("hello");
                break;
            }
            case 113318802: {
                if (!s.equals("world")) break;
                System.out.println("world");
            }
        }
    }
}
Copy after login
Copy after login

Compared to Jad, CFR has many parameters, which is still the code just now. If we use the following command, the output result will be different:
E :\CRF>java -jar cfr_0_125.jar Decompilation.class

/*
 * Decompiled with CFR 0_125.
 */
package com.yveshe;

import java.io.PrintStream;

public class Decompilation {
    public static void main(String[] args) {
        String str;
        String s = str = "world";
        switch (s.hashCode()) {
            default: {
                break;
            }
            case 99162322: {
                if (!s.equals("hello")) break;
                System.out.println("hello");
                break;
            }
            case 113318802: {
                if (!s.equals("world")) break;
                System.out.println("world");
            }
        }
    }
}
Copy after login
Copy after login

--decodestringswitch means to decode the details of switch support string.

Similar ones include --decodeenumswitch, --decodefinally, --decodelambdas, etc.

--decodelambdas can decompile lambda expressions.

JD-GUI

JD-GUI is a Java decompilation tool developed in C. It was developed by Pavel Kouznetsov and supports Windows, Linux and Apple Mac Os. platform. And provides the plug-in JD-Eclipse under the Eclipse platform. JD-GUI is based on the GPLv3 open source license and is completely free for personal use. The main thing of JD-GUI is to provide visual operations. You can drag and drop files directly into the window. The rendering is as follows

How to decompile java

JadClipse

Install the Jad plug-in in Eclipse. Note that the Jad plug-in is installed here, not the Jd plug-in~

Required resources: net.sf.jadclipse_3.3.0.jar plug-in jar and JAD.exe decompilation software (in There is a download address at the end of the article)

JadClipse download address Download the jar package of the plug-in from the official website, and then place the jar package in the plugins directory of eclipse; open Eclipse, Eclipse->Window->Preferences-> Java, at this time you will find that there will be one more JadClipse option than before to configure JadClipse as shown below:

How to decompile java

How to decompile java

##After the basic configuration is completed, We can set the default opening method of class files:

Eclipse->Window->Preferences->General->Editors->File Associations We can see that there are two ways to open class files. First, set the Class File Viewer that comes with JadClipse and Eclipse here, and JadClipse is the default.

All configurations are completed. Now we can view the source code. Select the class you want to view and press F3 to view the source code. If JadClipse is not the default setting, just set it to the default setting.

More For more java knowledge, please pay attention to the

java basic tutorial column.

The above is the detailed content of How to decompile java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template