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; } } }
Execute the following two commands:
javac Decompilation.java javap -c Decompilation.class
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 }
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; } } }
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"); } } } }
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"); } } } }
--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
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:
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!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment