Wednesday, August 23, 2006

Decompiling Java Class Files On-The-Fly

Being a Java developer, I sometimes want to look at the source code of class files just by double-clicking on the class file itself. There are lots of programs out there, such as DJ Java Decompiler, which allow you to do just this, but I was looking for a quick and easy way without the overhead of having to install memory intensive software.

Create ClassViewer.bat

I came up with this simple Windows batch script which uses the Jad Decompiler to decompile any Java class file that is clicked on. It then displays the decompiled code in your favourite text editor e.g. TextPad.

jad -p "%*" > "%*"
"C:\program files\TextPad 4\TextPad.exe" "%*"
sleep 5
del "%*"

Save this script as ClassViewer.bat

Associate Class files with ClassViewer.bat

Open File Explorer and click on Tools > Folder Options > File Types. Scroll down to the CLASS Extension (or if it is not there, add it by pressing the New button) and then press Change. A new window will open up asking you to select the program you want to use to open files of this type. Browse to ClassViewer.bat and hit OK. You have now associated this program with files with the .class extension.

Test it out...

Double-click a Java class file and see it execute the script and display decompiled code in your editor! You can even double-click class files within jars!

1 comment: