Home > Java, Tools > JAD – Java Decompiler

JAD – Java Decompiler


When I first found out that Java class files could be decompiled I was shocked at how much my code is “unsafe”. That was several years ago. After getting over myself I understood the power in allowing the world to view my code and to view the code of the rest of the world – particularly that of the Java framework. I got introduced to a very nice tool named ‘JAD‘ (AFAIK it stands for Java Decompiler). The tool had the ability to decompile Java class files easily but had problematic defaults. I had to wrap it in a utility batch file to make it easier to work with – so I sat with two colleagues and together we compiled this nice batch file:

# General variables used later in the script
set JAD="c:\Utils\jad.exe"
set EDITOR="c:\Utils\Notepad++\notepad++.exe"
set TEMP_DIR=c:\utils\decompiled

# Part 1 - Take the name of the .class file and calculate the full path of the decompiled .java file
set CLASS_NAME=%1%
set CLASS_NEW_NAME=%CLASS_NAME:"=%
set JAD_FULL_NAME=%CLASS_NEW_NAME:class=java%
set JAD_NAME=%JAD_FULL_NAME:\= %
for %%i in (%JAD_NAME%) do set JAD_NEW_NAME=%TEMP_DIR%\%%i

# Part 2 - Decompile the .class file and redirect the output of the decompilation into the target .java file
%JAD% -lnc -p %CLASS_NAME% > %JAD_NEW_NAME%

# Part 3 - Open the selected text editor in the background with the decompiled .java file
START /B %EDITOR% "%JAD_NEW_NAME%"

To use it you must perform two steps:

  1. Change the paths to whatever paths you like on your PC
  2. Open a .class file with your file browser and associate in windows that .class files should be opened with this batch file.

Once done – anytime you open a .classfile by double clicking on it or right-click and choosing ‘Open’ it will invoke the script and open the decompiled source. Script explanation in steps:

  1. Set a few variables that will be used later in the script
  2. Take the name of the .class file and calculate the full path of the decompiled .java file
  3. Decompile the .class file and redirect the output of the decompilation into the target .java file
  4. Open the selected text editor in the background with the decompiled .java file

The script and ‘JAD‘ executable can be downloaded from my public share at: JAD.zipJAD is no longer being maintained by anyone to the best of my knowledge – so I hope it will not disappear (although there are plenty of alternatives). Two useful links about JAD:

Advertisements
Categories: Java, Tools Tags: , , ,
  1. He
    April 6, 2012 at 20:21

    Awesome script!

    When there are spaces in the path the script doesn’t work. To fix this just surround with double-quotes:

    # Part 2 – Decompile the class file and redirect the output of the decompilation into the target .java file
    %jad% -lnc -p “%CLASS_NAME%” > “%JAD_NEW_NAME%”

    # Part 3 – Open the selected text editor in the background with the decompiled .java file
    START /B %EDITOR% “%JAD_NEW_NAME%”

    • April 8, 2012 at 13:26

      Thank you for your comment!
      If the target for the decompilation contains spaces (e.g. “c:\utils\my decompiled classes”) – than using “%JAD_NEW_NAME%” will solve the problem. I will update it shortly.

      When I tried using “%CLASS_NAME%” it failed, since it seems that windows already took care of it when passing the value when I associated .class files with the OpenClass.cmd script. If you use it from the command line directly, you will need to add the quotation marks yourself.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: