139

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.
4
  • 8
    Just a semantics thing, shouldn't it be one = not two? == is a question while = is a statement and you are making a statement about your environment.
    – icco
    Sep 16, 2008 at 20:08
  • 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, 2008 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, 2008 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, 2013 at 6:44

17 Answers 17

227

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

2
  • 1
    How up to date is this answer in 2020? Mar 24, 2020 at 20:36
  • As long as the link goes somewhere, I suppose it's good. I wouldn't know if there's now a better Java decompiler, as I haven't used Java since long ago.
    – DarenW
    Mar 30, 2020 at 20:20
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
0
20

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
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.

3

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

3

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

0
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.

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 :)

0
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....

1

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

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.

1

JAD and/or JADclipse Eclipse plugin, for sure.

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

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.

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).

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.