141

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

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

  • Environment == Windows w/ VS 2008 installed.
Pops
  • 30,199
  • 37
  • 136
  • 151
cpuguru
  • 3,763
  • 5
  • 33
  • 31
  • Can you clarify this question? My immediate thought was that you wanted to read the actual bytecode, but it seems that you actually want to decompile someone else's code. – Tim Frey Sep 16 '08 at 20:39
  • 14
    @icco, = means assignment, == means equality. He is saying that (Environment == Windows w/ VS2008 installed) is true, not giving an order to set Environment to Windows w/ VS2008 installed. Man that was offtopic and nerdy. – Blorgbeard Sep 16 '08 at 20:47
  • 7
    For the record == doesn't declare equality; it tests equality. You can write true == false, and it's about as meaningful as writing Environment == Windows w/ VS 2008 installed, i.e. not meaningful at all. – bwerks Mar 21 '13 at 06:44

17 Answers17

228

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

DarenW
  • 16,549
  • 7
  • 63
  • 102
86

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
  • 188,989
  • 46
  • 291
  • 292
22

As pointed out by @MichaelMyers, use

javap -c <name of java class file> 

to get the JVM assembly code. You may also redirect the output to a text file for better visibility.

javap -c <name of java class file> > decompiled.txt
Nilashish C
  • 375
  • 2
  • 11
7

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
  • 2,628
  • 21
  • 13
3

you can also use the online java decompilers available. For e.g. http://www.javadecompilers.com

Prachi Sharma
  • 71
  • 1
  • 6
3

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

Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
2

jd-gui "http://code.google.com/p/innlab/downloads/detail?name=jd-gui-0.3.3.windows.zip&can=2&q=" is the best and user friendly option for decompiling .class file....

2

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
  • 1,407
  • 10
  • 12
2

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
  • 135
  • 1
  • 10
1

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

cynicalman
  • 5,861
  • 3
  • 30
  • 30
1

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 Gochenour
  • 253
  • 1
  • 7
1

JAD and/or JADclipse Eclipse plugin, for sure.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
1

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
  • 6,056
  • 4
  • 39
  • 48
0

CFR - another java decompiler is a great decompiler for modern Java written i Java 6.

tidbeck
  • 2,363
  • 24
  • 35
0

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

0

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
  • 54,515
  • 14
  • 108
  • 120
0

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
  • 2,169
  • 1
  • 15
  • 12