views:

6638

answers:

14

I'm trying to figure out what a Java applet's Class file is doing under the hood. Opening it up w/ Notepad or Textpad just shows a bunch of gobbley-gook.

Any way to wrangle it back into a somewhat readable format so's I can try to figure out what it's doing?

  • Environment == Windows w/ VS 2008 installed.
+3  A: 

Using Jad to decompile it is probably your best option. Unless the code has been obfuscated, it will produce an okay result.

Rasmus Faber
+1  A: 

That's compiled code, you'll need to use a decompiler like JAD: http://www.kpdus.com/jad.html

cynicalman
A: 

You want a java decompiler, you can use the command line tool javap to do this. Also, Java Decompiler HOW-TO describes how you can decompile a class file.

Drew Frezell
+1  A: 

what you are looking for is a java de-compiler. I recommend JAD http://www.kpdus.com/jad.html It's free for non commercial use and gets the job done.

Note: this isn't going to make the code exactly the same as what was written. i.e. you're going to lose comments and possibly variable names, so it's going to be a little bit harder than just reading normal source code. If the developer is really secretive they will have obfuscated their code as well, making it even harder to read.

David Beleznay
A: 

As suggested you can use JAD to decompile it and view the files. To make it easier to read you can use the JADclipse plugin for eclipse to integrate JAD directly to eclipse or use DJ Java Decompiler which is much easier to use than command line JAD

+6  A: 

If you don't mind reading bytecode, javap should work fine. It's part of the standard JDK installation.

Usage: javap <options> <classes>...

where options include:
   -c                        Disassemble the code
   -classpath <pathlist>     Specify where to find user class files
   -extdirs <dirs>           Override location of installed extensions
   -help                     Print this usage message
   -J<flag>                  Pass <flag> directly to the runtime system
   -l                        Print line number and local variable tables
   -public                   Show only public classes and members
   -protected                Show protected/public classes and members
   -package                  Show package/protected/public classes
                             and members (default)
   -private                  Show all classes and members
   -s                        Print internal type signatures
   -bootclasspath <pathlist> Override location of class files loaded
                             by the bootstrap class loader
   -verbose                  Print stack size, number of locals and args for methods
                             If verifying, print reasons for failure
Michael Myers
that's so cool. i can't believe i didn't know about this! oh, need to go play...
Stu Thompson
A: 

JAD is an excellent option if you want readable Java code as a result. If you really want to dig into the internals of the .class file format though, you're going to want javap. It's bundled with the JDK and allows you to "decompile" the hexadecimal bytecode into readable ASCII. The language it produces is still bytecode (not anything like Java), but it's fairly readable and extremely instructive.

Also, if you really want to, you can open up any .class file in a hex editor and read the bytecode directly. The result is identical to using javap.

Daniel Spiewak
+25  A: 

jd-gui is the best decompiler at the moment. it can handle newer features in Java, as compared to the getting-dusty JAD. http://java.decompiler.free.fr/

DarenW
Whoah! I just tried this (lost my laptop and needed to get some sources back in a hurry) and it was incredible.
Marcus Downing
wow, great! i'm astonished how helpful this answer has been for others. the vote slowly crawls upward... and i'm not even a java expert! but i do love good tools.
DarenW
A: 

You need to use a decompiler. Others have suggested JAD, there are other options, JAD is the best.

I'll echo the comments that you may lose a bit compared to the original source code. It is going to look especially funny if the code used generics, due to erasure.

Zac
A: 

JAD and/or JADclipse Eclipse plugin, for sure.

John Gardner
A: 

There is no need to decompile Applet.class. The public Java API classes sourcecode comes with the JDK (if you choose to install it), and is better readable than decompiled bytecode. You can find compressed in src.zip (located in your JDK installation folder).

Arno
A: 

If the class file you want to look into is open source, you should not decompile it, but instead attach the source files directly into your IDE. that way, you can just view the code of some library class as if it were your own

arturh
+1  A: 

You can try DJ Java Decompiler: http://members.fortunecity.com/neshkov/dj.html or http://www.neshkov.com/

DJ Java Decompiler is Windows 95/98/Me/NT/2000/XP/2003/Vista decompiler and disassembler for Java that reconstructs the original source code from the compiled binary CLASS files (for example Java applets).

+1  A: 

cpuguru, if your applet has been compiled with javac 1.3 (or less), your best option is to use Jad.

Unfortunately, the last JDK supported by JAD 1.5.8 (Apr 14, 2001) is JDK 1.3.

If your applet has been compiled with a more recent compiler, you could try JD-GUI : this decompiler is under development, nevertheless, it generates correct Java sources, most of time, for classes compiled with the JDKs 1.4, 1.5 or 1.6.

DarenW, thank you for your post. JD-GUI is not the best decompiler yet ... but I'm working on :)

Emmanuel Dupuy