« Evolving JMX: on the cover of WebSevices Journal | Main | Upgrading JMX: JMX1.2 --> JMX2.0 »

A little script to disassemble jar files

My favorite Java decompiler Jad was having trouble decompiling J2SE5.0 class files, forcing me to use the bundled disassembler javap. The Java assembly code it generates is no match for source code generated by Jad, but is not too bad if your goal is just to understand some observed behavior and the supplied documentation is not sufficient (which is not exactly a rarity!). What I did find irritating about javap is its inability to handle directory tree and propensity to dump the disassembled code on standard output (so if I tried to disassemble multiple class files in one command and redirected the output to a file, all I get is one single big fat file with idsassemble code for all the classes).

These limitations motivated me to write jarp.pl, a little perl script, to disassemble all classes of a jar file and produce a directory tree of appropriately named files with disassembled code (to get this script, download jarp.txt and rename it to jarp.pl). It worked quite well on a number of jar files (including rt.jar of J2SE5.0) on my Windows box.

It takes the filename of the jar file as an argument, unjars it in a subdirectory named classes (provided this subdirectory doesn't exist), and then invokes javap for each of the .class files, saving the output in the corresponding .jasm file under jasm subdirectory. Goes without saying that you should have Perl installed in your machine (you can Perl for Windows from ActiveState).

A sample session with jarp.pl is shown below:

C:\myhome>set java_home=c:\j2se\j2sdk5.0 C:\myhome>dir Volume in drive C has no label. Volume Serial Number is 14A6-C5D4
Directory of C:\myhome
11/02/2004 07:10p <DIR> . 11/02/2004 07:10p <DIR> .. 11/02/2004 06:35a 1,762 jarp.pl 1 File(s) 1,762 bytes 2 Dir(s) 53,062,135,808 bytes free
C:\myhome>perl jarp.pl %java_home%\jre\lib\jce.jar Unjarring C:\j2se\j2sdk5.0\jre\lib\jce.jar in directory classes ... ... done. Making list of classes ... ... done. jasm/javax jasm/javax/crypto jasm/javax/crypto/interfaces jasm/javax/crypto/spec jasm/META-INF [0] Disassembled: jasm/javax/crypto/BadPaddingException.jasm [1] Disassembled: jasm/javax/crypto/Cipher.jasm ... Output Skipped ... [60] Disassembled: jasm/javax/crypto/spec/RC5ParameterSpec.jasm [61] Disassembled: jasm/javax/crypto/spec/SecretKeySpec.jasm
C:\myhome>dir Volume in drive C has no label. Volume Serial Number is 14A6-C5D4
Directory of C:\myhome
11/02/2004 07:12p <DIR> . 11/02/2004 07:12p <DIR> .. 11/02/2004 07:12p <DIR> classes 11/02/2004 06:35a 1,762 jarp.pl 11/02/2004 07:12p <DIR> jasm 1 File(s) 1,762 bytes 4 Dir(s) 53,057,335,296 bytes free

I have found it to be a very useful tool. Hope it helps you as well!


This page contains a single entry from the blog posted on November 2, 2004 7:31 PM.

The previous post in this blog was Evolving JMX: on the cover of WebSevices Journal.

The next post in this blog is Upgrading JMX: JMX1.2 --> JMX2.0.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33