Home Java JavaBase How to decompile java

How to decompile java

Dec 07, 2019 pm 03:06 PM
java

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles