Learn how to download applets and decompile Java class files - Part 2  
Written by
Developer's Daily Java Education Center

Introduction

In our previous article, we demonstrated a simple method that can be used to easily download the class files of Java applets. In this article we'll show you how to decompile the Java class, turning the .class file back into a .java source code file.
 

Why do this?

If you're just joining us in Part 2 of our series, I feel the need to explain why we're doing this series in the first place:

  1. First, we're a Java educational center.  Telling you the bad about Java as well as the good is also educational.  Whether it's bad or good, it is truth.  The fact is that other people can decompile your Java classes, and you need to know that, and it's helpful to know how they can do it.
  2. Second, and even more importantly, if you know how to decompile Java classes, you can also learn how to try stop it (assuming you're interested in stopping it).

Obtaining software to decompile Java class files

To decompile a Java class file, you're going to need some software specially made for decompiling Java class files (also called reverse-engineering).  In this article we're going to show you how to decompile Java class files with a software package that goes by the name of Mocha.

Mocha is probably not the best decompiling software available today, but it is one of the first packages available - if not the first - and it's also free.  Mocha is written in Java, so you'll also need a copy of the Java JDK, or the JRE.

Step 1: Download Mocha

Step 2: Install Mocha

Decompiling a Java class file

In this article we're going to decompile the AnimatedAd.class Java class file we downloaded in our first article.  If you're at all familiar with our web site, you might know that the original source code for this class file is also available.  This is good, because it gives us a chance to compare the original source code to the decompiled version of the source code.

If you're ready, let's begin the process of decompiling the class file.  If you've installed Mocha properly, there's really very little to the process other than running Mocha.
 

Step 1:  Open a DOS or Unix window

Step 2:  Modify your CLASSPATH variable Step 3:  Copy the class file to the Mocha installation directory Step 4:  Run the Mocha program  
    Decompiling AnimatedAd.class -> AnimatedAd.mocha 
     Method init.................................................................... 
    ................................................................................ 
    ................................................................................ 
    ................................................................................ 
    ................................................................................ 
    ........................ 
     Method start................ 
     Method stop.............. 
     Method run..................................................................... 
    ......................................................................... 
     Method mouseDown............... 
     Method pause........ 
     Method drawMyString.............. 
     Method paint................ 
     Method numStringsUsed......................... 
     Method getRandomInt................................................. 
     Method <init>............................. 
     
 
Figure 1: This is the output Mocha generated and printed to my DOS window.
   
 

The decompiled source code

When Mocha runs, it also creates an output file named AnimatedAd.mocha.  This file contains the decompiled Java source code - the source code it created by reading the AnimatedAd.class binary file.

The AnimatedAd.mocha file is too large to list here, but you can click here to view the reverse-engineered source code.

As a point of comparison, you can click here to view the original AnimatedAd.java source code.
 



 
What's next

As you've seen from this example, reverse-engineering a Java class file into a Java source code file appears to be pretty easy.  But what about the quality of the reverse-engineering process?  In our next article, we'll evaluate the results of the decompiling process by comparing our original source code to the decompiled code.  We'll also discuss other modern tools that can be used for reverse engineering Java class files.  Mocha has a few bugs that you may not see immediately, so we'll introduce a couple of tools that have followed Mocha's path.

Finally, at the conclusion of our series, we'll show you how you can use tools to protect your code from others that may want to try their hand at reverse-engineering your class files.

Return to our Developer's Daily Pure Java Education Center
Return to our Developer's Daily Home Page

(Editor's Note: The process of decompiling Java class files belonging to other businesses or individuals may be illegal in your city, state, or country. Frankly, I don't know for sure, because, to coin a phrase from Star Trek's (TM) Doctor McCoy, "I'm an editor, not a lawyer." In any case, this series of articles is not written to encourage that practice. These articles are presented only so you can learn to protect your own Java class files from reverse engineering.)


What's Related


Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.