How to decompile a Java .class file from rt.jar - java

I have extracted a .class file from the rt.jar file in jre1.7, and have attempted to open it using notepad++, however it is unreadable. How can I decompile the .class file into a .java file so I can view the code?
Please note that I am interested in the decompilation of the code, not viewing the source code provided in the JDK downloads.

As #Jon Skeet said, don't use JRE but JDK, which contains the entire source code. If you for some reason insist on your approach, then I recommend JD-GUI for decompilation.

.class files are the compiled binary files. You can't read the Java code from them, but you can run them. If you want to see the source you usually need .java files. These are either packed in the same jar but then the src folder, or they can be downloaded in a seperate package.

You can use any decompiler tool like cavaj for decompiling a class file to source code (java file). I'm using it, and it works perfectly.

Try any of the decompilers listed here:
Procyon - https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler
CFR - http://www.benf.org/other/cfr/
JD - http://jd.benow.ca/
Fernflower - https://github.com/fesh0r/fernflower
Or use online decompiler - http://www.javadecompilers.com/

Related

How to get source code from .jar file

I have my project in .jar file.
But i lost the source code of it.
How to get source code from .jar file.
First of all I would like to tell you that if your project is big and complex, you are in trouble. Generated source code via external tools(no matter whatever the tool is) is never same as the real code. Code like comments, constants, inner classes, etc gets messy.
For simpler code and projects you can use -
Java Decompiler (JD-GUI) http://jd.benow.ca/
DJ Java Decompiler http://www.neshkov.com/dj.html
But always know this that its not what was written originally in the source code.
You must decompile your code. You can use Java decompiler http://jd.benow.ca/

Why don't I see code when viewing included Jar libraries in Eclipse?

There doesn't seem to be any code there. I expected to see class declarations so I could see what the code does but instead there's some
Are they somehow precompiled? What's the difference between included Jar file and a pure code?
You are looking at .class file, which is a generated when you compile .java file. To see what the program does, you have to look into .java file. You can refer official java documentation for that.
A .jar file is packaged file with .jar extension, it contains compiled java files and their class files. This file is usually imported into projects to use the classes defined in that package.
You can use "jar xf jar-file" command in command-prompt/terminal to extract the files from jar and look into the package.
A JAR will normally contain compiled class files. It may also contain source files or there may be a separate JAR that contains the source files, but not necessarily so.
If you want to use the library in your project, then a JAR of compiled class files is what you want. If you want the source code, then you'll have to see if it is available from wherever you downloaded this from. If all you want is to see how to use the classes, then probably what you want are JavaDocs for the library you are using. This is an HTML based API documentation.
Well, this is because you haven't attached any source for the mentioned dnsns.jar. You can attach source to existing JAR files in Eclipse. Refer this SO post: Is there an easy way to attach source in Eclipse?
For this specific dnsns.jar, it is part of your JRE, and if you are not able to see its source in your IDE, then it means that the Java that you have setup in IDE lacks the source. If your installation does not have the source (src.zip), then you can get it manually as mentioned on this SO post: Where to find Java JDK Source Code?
EDIT: Alternatively, you can also use a decompiler (e.g. http://jd.benow.ca/) to reverse engineer the source from byte code, though, it may not be the exact match to the original source but you can understand the overall idea. You can add the decompiler as the default program for opening .class files in eclipse Windows > Preferences > General > Editors > File Associations. Select *.class filter and add your decompiler as the program. Though, it is not as clean as attaching the source to JAR, but may work if you don't have access to source.
EDIT2: About your question
What's the difference between included Jar file and a pure code
Eclipse can find .java files for your own code because obviously they are in your workspace. But when you add a JAR file as library, it may have the source (.java) in it or not. If the source is available, eclipse can display it by default. If not, you have to add it manually.

How can I see the source code of packages in Eclipse?

i started to learn android program ,and i curious to see the code of the classes/
for example the class of import android.app.Activity.
how can i see the source code of the packages in eclipse?
You can attach source code to libraries. yourProject / Properties / Java Build Path / Libraries / yourLibrary / Source attachment, and then enter the folder or zip file containing the source code (which you need to download separately).
If you only have the object code (.class files), then you need to learn to read ByteCode (which Eclipse shows pretty nicely) or use a Java ByteCode disassembler.
As in android sdk what we get is a compiled jar file which contains all the classes.
So in eclipse you can't read the source, but you may use, The official online version to read the source code of classes:
http://developer.android.com/reference/packages.html
[In eclipse although by CTRL+Click on import may show you a bit about the class, although that won't be easy to understand :)]

How to dump contents of a Java library to the console?

Is there a tool for dumping the contents of a library or JAR file to the console?
I'm looking for the Java counterpart of DUMPBIN /SYMBOLS, which works on Windows native-mode libaries. With C# and the rest of .NET, there are a bunch of visual tools like .NET Reflector and the object browsers built into Visual Studio. All-in-all, I prefer command-line tools, but I will be grateful anything that works :)
(I'm just getting started with Java and Scala on Windows.)
The command you're looking for is:
jar tf <jarfilename>
You can unpack a JAR file (jar xf my.jar) to see the contents; if you want to do this within your code, there are tools in java.util.jar for working with JAR fileS.
Alternatively, if you know part of the package hierarchy, you can type part of it in the Scala REPL and hit tab to get a list of valid completions. For example:
scala> java.util.jar. <-- hit tab here
Attributes JarEntry JarException
JarFile JarInputStream JarOutputStream
JarVerifier JavaUtilJarAccessImpl Manifest
Pack200
However, it's awfully hard to figure out how to use things without API documentation. Your first step should almost always be to try to find the documentation and look at them instead.
If you know the class name:
$ javap -classpath jarfile some.package.in.that.jarfile.ClassName
Also you can "script-ize" it so that it iterates through all the classes in the jar file and run javap for each one
Also note that a JAR file is merely just a zipped file so any ZIP utility would do. And most IDEs offer to explore inside the JAR libraries you add to a project (Eclipse for instance).
As mentioned, JAR files are ZIP files and can be extracted in the same way.
To browse the classes inside a JAR file, I recommend you to use an IDE. Add the JAR file as a library dependency to your project, and then you should be able to browse through the classes and methods.
How about obtaining the source code for the JAR file (if that's possible)?

convert .class file/s to .java file/s of .jar file

how can i convert?? .class file to .java file/s to see source code of .jar file (which is mobile app on midp 2.0)
how can i convert ?
I would suggest using a decompiler such as JAD. However, if the code has been obfuscated, this may not be very useful.
Use a Java decompiler like this.
Adding to the previous answers: recently, a new wave of decompilers is coming, namely Procyon, CFR, JD, Fernflower
Here's a list of decompilers as of 2015:
Procyon
CFR
JD
Fernflower
You may test above mention decompilers online, no installation required and make your own educated choice.
Modern java decompilers in the cloud: http://www.javadecompilers.com/

Categories