Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Decompiling Classes in Java

$
0
0

1. Introduction

In this tutorial, we'll discuss decompiling Java classes. When source code is not available, decompiling Java classes helps to debug and understand source code behavior.

Let's take a look at the different options available.

2. Decompiling in IDE

Since most development is done in an integrated development environment (IDE), it makes sense that decompilation should also take place in an IDE.

For more info on the IDEs we will work with, check out our articles on how to debug in Eclipse and configuration for IntelliJ IDEA.

2.1. Eclipse

Firstly, in Eclipse we need a plugin such as the Enhanced Class Decompiler (ECD). This plugin uses five different decompilers.  We can install it from the Eclipse Marketplace and then we need to restart Eclipse.

Next, ECD requires a small amount of setup to associate class files with the Class Decompiler Viewer:

Also, we need to associate “.class without source” files:

Finally, we can use the decompiler by pressing Ctrl+Left-Click on a class name. We see the decompiler used on the file tab in brackets.

In this example, we're using FernFlower:

2.2. IntelliJ IDEA

In contrast to Eclipse, IntelliJ IDEA provides the FernFlower decompiler as a default.

To use it, we simply Ctrl+Left-Click on a class name and view the code:

Also, we can download the source. Downloading the source will provide the actual code and comments.

For instance, the Component annotation class from the above screenshot includes Javadoc on the use of Component. We can notice the difference:

While decompilation is very helpful, it doesn't always give a complete picture. The full source code gives us a complete picture.

3. Command Line Decompiling

Before IDE plugins, the command-line was used for decompiling classes. Command-line decompilers can also be useful for debugging Java bytecode on a remote server that is not accessible with an IDE or GUI.

For example, we can decompile with JDCommandLine using a simple jar command:

java -jar JDCommandLine.jar ${TARGET_JAR_NAME}.jar ./classes

Don't leave off the ./classes parameter. It defines the output directory.

After successful decompilation, we can access the source files contained in the output directory. They're now ready to view through a text editor like Vim.

4. Conclusion

We looked at decompilation in Eclipse and IntelliJ IDEA IDEs as well as a command-line option when they aren't available.

We also looked at the difference between linking source code and decompilation.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>