Recursive Java Decompiler

30th May 2007

Often we encounter situations where the only solution to a certain Java problem is to decompile the contents of an entire JAR file, edit the resulting the source, rebuild and repackage before using.

I searched a lot for decompilers which could recursively decompile multiple class files while preserving the package hierarchy, but in vain. So, I thought of writing a Windows batch script myself, to do what I was looking for. This script is also known as JAR decompiler or recursive JAD.

Download Recursive Java Decompiler  175 kB Downloaded times


I have used JAD for decompilation. JAD is an excellent decompiler which can be downloaded from the author's website, http://www.kpdus.com/jad.html. It provides an executable, jad.exe which has various options to run the decompiler.

The following steps outline how to make use of the Recursive Decompiler Utility to successfully decompile a huge set of class files.

  • Download decompile.zip and extract the contents into a path on the file system that doesn't contain spaces. Eg. D:\decompile.
  • You should be able to see a directory structure as shown below.
  • Extract the contents of the JAR file you would want to be recursively decompiled, into the folder decompile\classes. You may use any the command "jar -xvf <jar> <dir>" or any of the popular file compression softwares such as WinZip or WinRar to do the same.
  • Make sure the decompile\src folder is empty.
  • Run decompile.bat.
  • The utility decompiles each of the .class file found in classes directory and places the .java files under the src directory. This operation preserves the original class hierarchy.

Now you can edit the code, recompile, rebuild, repackage and start using it!


Note: Because this utility uses some advanced batch scripting commands, it works only on Windows XP or higher operating systems.


What others say

Sriram K 4th June 2007
This is good.. great stuff. Thanks. I am not good at batch scripting, but is handling spaces in the folder structure a big problem?

Adarsh R 4th June 2007
Thanks Sriram. If paths were directly used in batch files without any manipulations, it would have worked well and fine. However, in this batch script, I'm manipulating the path in the for loop to trim the ".class" extension so that I could use a ".java" one. While doing this, I was facing some problems if I hadn't placed the script in a path without spaces.

M. Rettkowski 21st August 2007
Great job, BUT I firstly executed the batch file from <C:>. Any *.class file existing on my harddrive was going to be deleted. Is there a possibility to exit the batch if "cd src" (or any other command) fails?

shivananda 18th September 2007
hi, i'm trying to do decompile some folder structure it is giving jad.exe error and in cammand prompt it showing

Not all exception handlers will be in the method main resolved
Couldn't fully decompile method main

please help me Adresh..
thanks & regards
shivu..

Adarsh R 19th September 2007
@M. Rettkowski
That sounds scary! I will try to do something about it. Thanks.

@shivananda
This error is something which JAD itself throws and is unfortunately, out of my scope. Try replacing the jad.exe with the latest one from the author's website. It might help. Here's the link: http://www.kpdus.com/jad.html

giuseppe 18th October 2007
@Adarsh R
U R great! That really helped a lot, I already had had to do with jad, but it had always been a torture to decompile complicated packages;)

@shivananda
I replaced jad with a newer one I already had worked with. Works better, but if You look at jad's Author's site, You will find out, there are certain limits not only to jad but to reverse engineering in general.

Sunil 27th June 2008
This bat-file work like a charm. But i think the space problem in paths can be avoided.

i just modified your bat like this :

@echo off

echo Copying .class files...

xcopy /Q /E /Y classes src

cd src

echo Decompiling .class files into .java files...

::for /R src %%a in (*.class) do jad -d %%~dpa -o -s .java "%%a"

..\jad.exe -r -o -sjava -d . ./**/*.class

echo Deleting .class files...

::cd src

del /S /Q *.class

cd..

pause

ie, after copying all class-files from classes-folder give 'cd src'. then issue this command '..\jad.exe -r -o -sjava -d . ./**/*.class' to recursively decompile (- option of JAD [restore package directory structure]).

this is working fine.

sunil

Adarsh R 29th June 2008
Thanks a lot Sunil for the changes. I will soon update the distributable.

Sunil 30th June 2008
I am very glad to know that you considered my suggestion. Also i am planning to make a Swing UI to do this recursive JAR decompilation (so, one can specify JAR file and destination directory and just say 'Decompile').


Let me know what you think

Thank you. Please note that comments are moderated and will take some time to show up here.
Name (required)
Email (required but never published)
Website
Your Comments (2000 characters max) Characters left: 2000