Skip to content

Intro and Java Class File Decompilation

I started this page just to serve as a blog for things of interest that I encounter in my daily profession. I hope that it is educational and useful.

For this first entry, I’m going to talk about Java source decompilation. Yes, it can be used for evil, but it can also be helpful when dealing with third-party resources that offer little to no documentation. It doesn’t even have to be third-party, it may be some jar file that was created in-house 5 years before you joined the project team. Sometimes it’s just nice to see the source code to figure out what’s going on.

You can decompile the source code form a jar in 3 easy steps.

1. Install and configure JAD. Download JAD from http://www.kpdus.com/jad.html . Place the JAD executable in a directory and add that directory to your PATH variable.

2. Create a script that will run JAD to decompile .class files and to rename those files from .jad files to .java files. I wrote the script below and named it dcJava.

#!/bin/bash
#decompile class files using jad
echo ‘Decompiling Class files using Jad’
for file in *.class; do jad $file; done
#rename from .jad to .java
echo ‘Renaming file extensions’
for file in *.jad ; do mv $file `echo $file | sed ‘s/\(.*\.\)jad/\1java/’` ; done
#get rid of the class files
echo ‘Removing Class files’
rm -rf *.class
echo ‘Done.’

3. Explode the JAR and run the script above in the directories where the .class files are.

Note: The script above will delete the original .class files. This shouldn’t be a big deal, if you still have the original JAR.

The code that JAD generates should be fully functional. However, there will be lexical differences between the actual source code used to produce the .class file and the code that JAD generates.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

2 Comments

  1. Andre wrote:

    Will this work if I only have the .jad and the .jar file?

    Wednesday, January 9, 2008 at 8:01 am | Permalink
  2. eokuwwy wrote:

    Hi. Sorry for the late response. It would work if you exploded the jar first. (i.e. jar -xf )

    However, if you really have the .jad file or files already, then all you need is this line that renames them as .java files.

    for file in *.jad ; do mv $file `echo $file | sed ’s/\(.*\.\)jad/\1java/’` ; done

    Wednesday, March 12, 2008 at 2:26 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*