How to de-compile a .class file? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I “decompile” Java class files?
I m new to java and i was wondering if there is a way to retrieve the source code of a file (i.e. the .java file) from its compiled file (.class file).
I have read about the compilation process i.e. about the parsing, the syntax and semantics trees, the intermediate code generation, macros used etc. It sounded like a very complex process. Reverse engineering it will be even more difficult. Therefore, I think it is not possible but i had to ask. I googled it but couldn't find anything satisfactory.
Also i learnt about code-obfuscation in my class. If de-compilation is possible how will it behave for an obfuscated file?
Thanks in advance.

[edit : I won't complete the answer as this question is a duplicate, this is just an additionnal comment on the second part of the question]
When you obfuscate a class, you remove everything that isn't needed for the execution but is present in the file, like the names of the method.
In fact, obfuscating an isolated class doesn't remove a lot because other classes need the names of the methods or visible fields. So usually you obfuscate a group of class togethers using a tool like proguard.

Yes there is, very simple by the way, get a program called DJ Java Decompiler (search in google it's easy to find) and after you install it, you just have to open the .class file, and it shows the code all formatted and indented.
Microsoft Windows only application

Related

How do I change the package name of classes inside a JAR? [duplicate]

This question already has answers here:
Hand Edit A Jar to Change Package Names
(6 answers)
Closed 7 years ago.
Typical problem - I have x-1.0.jar, x-2.0.jar and y-1.0.jar. I want to use x-2.0.jar and y-1.0.jar but y-1.0.jar depends on x-1.0.jar.
There is a tool out there which allows you to bundle up dependencies into a single JAR and change the package names internally so it avoids conflict. For example, if "x" contains software in the package "com.foo", then it can change all classes in x-1.0.jar to be in package "old.com.foo", as well as any references in y-1.0.jar, so we could make a new JAR xy-1.0.jar; this JAR would not contain any publicly-exposed classes in the "com.foo" packages, so that we could feel free to use it alongside x-2.0.jar with no conflict.
The problem is, I have totally forgotten what the name of that tool is! We considered using it once in my company, but my management were concerned about doing binary-level modifications to classes. However now (several years later) we have a similar problem which only affects test code, so I want to propose it again. But I have totally forgotten the name!
Things I remember about it:
Open source hosted on Google Code
Based on ObjectWeb ASM underneath
I think the name may have been a pun of some description
Does anyone know what tool this might be? Please help me out!
I remembered it! The tool is called JarJar Links.

Opening and editing java classes in not java bytecode view

How to open and edit java class files? I've searched, but I either found how to open them without being able to edit them, or how to be able to edit only the bytecode. I want to be able to read and edit .class in the "normal" view. (as source code, not bytecode)
What you are asking for is basically impossible. The ".class" files do not contain source code, and do not contain enough information to reconstruct the original source code.
If you are a bit lucky, a good decompiler would be able to create compilable source code that means the same thing as the ".class" files. However:
That decompiled source code won't have the original comments.
The original names of any local variables are not recoverable.
The structure of the decompiled code may be different; e.g. string concatenation, for loops and try/catch structures may be transformed.
The code is not guaranteed to be correct, or compilable at all. (It depends on the decompiler, and how well it deals with the version of Java you are trying to decompile.)
And if the code you are trying to edit was obfuscated, then your chances of success are greatly reduced. An obfuscator deliberately transforms the ".class" files to remove useful information, and to confuse decompilers.
To my knowledge, no IDE supports editing of ".class" files like this.
Before my suggestion, I wanna point out that as far as I've searched, there has yet to exist a free decompiler that allows you to edit the source code produced, and save it automatically. I believe this is due to decompilers only being able to attempt to decompile the code, and the source code produced is not always exact/error free/compilable.
What you can do
You must use a decompiler, such as JAD, copy the source code that it produces and paste it into a new file.
As for the download link, you can find that on google, as I am unsure of the safest place to get it.
A decompiler does its best at converting the content (bytecode) within .class files into readable Java source code. Not only will some identifiers (method, variable and class names) will be replaced with generic names, meaning String name; in bytecode might decompile to String aString1;. Random variables might also be generated (tmp variables), which can lead to the produced code being unable to compile.

Hide a class in a .jar

Whenever I build my app all classes (logically) are visible in the .jar that comes out of it.
Aswell as a class that holds information to my MYSQL server (for the app to connect to). But I dont want this information to be publicly visible!
How can I "hide" this code or "hide" the class?
Thanks!!
I think you mean you dont want someone to do reverse engineering with your .class inside your jar file. There are many decompilers that can do that.
So you would need to Obfuscate your code with an obfuscator utility.
The process of obfuscation will convert bytecode into a logical
equivalent version that is extremely difficult for decompilers to pick
apart. Keep in mind that the decompilation process is extremely
complicated and cannot be easily 'tweaked' to bypassed obfuscated
code. Essentially the process is as follows:
Compile Java source code using a regular compiler (ie. JDK)
Run the obfuscator, passing in the compiled class file as a
parameter. The result will be a different output file (perhaps with a
different extension).
This file, when renamed as a .class file, will be functionally
equivalent to the original bytecode. It will not affect performance
because a virtual machine will still be able to interpret it.
Here is an article describing this process in more detail and
introducing an early obfuscator, Crema:
http://www.javaworld.com/javaworld/javatips/jw-javatip22.html

Is it possible to edit a pre-existing .class file from within my program?

This may seem like an odd thing to ask, but it'd take me forever to explain why I need it...
What I need is a way to edit a pre-existing Java .class file within its JAR file, with either a command prompt, or within my Python program. I need it to happen automatically, once the user pushes a button.
I have absolutely no clue how to do this, or if it's possible.
A jar file is a zip package, you need only to extract the file, edit the content and put it back. The harder part is how to edit the .class file. The java .class file is a binary format , there're several libraries may help you.
Yes you can do this. Now how you gonna do it depends upon what you want to do. For your cross-cutting issues look at AspectJ. Using AspectJ you can add your custom code even after the class is compiled.
You have a problem with this approach, if the class has already been loaded by a JVM classloader, as it may not actually reread the .class file again until the application has been rerun.
I know that there exists the BCEL but I've not used it, so I dont know if it can be used a) from python, or b) during runtime.
EDIT: Actually, Jeffrey's list is better as it provides a much more comprehensive list of Byte Code manipulators.

How to view the source code from jar file in my own JAVA GUI?

I have one GUI with one list box to display the list of methods in the class.
I can achieve it using reflection.
But can I view the source code in another text area on selecting the method name?
I knew about decompilers. but I don't want to see source code in their window.
I want to use some thirdparty lib so that I can see the source code of specific method in my own GUI.
Please suggest if there is an API available for this.
You will need a decompiler of some sort, that you can link to. I am not sure there are any libraries, but here's a link to the JD Java Decompiler.
Remember that you lose variable names and such during compilation, so if you decompile the resulting source code may be less readable.
If you have access to the source you could link it to the class files, and find the chosen method source in the source files linked. This can be achieved by a simple one-pass parse of the source files.
Your biggest problem will be determining when a method ends, and a simple solution is to count {'s and }'s and determine when the { of the method declaration is closed.
This is an old question, but seeing as the decompiler landscape has changed significantly in the past year, I feel it's worth resurrecting.
Procyon is an open source framework that contains, among other things, a Java decompiler. It is written in Java, and the decompiler APIs can be integrated into another application fairly easily. In fact, there are already two third-party GUI front-ends, including the SecureTeam Java Decompiler.
CFR does not have source code available yet, but it is also an excellent decompiler. It too is written in Java, and while I have not tried to integrate it with an existing application, it should certainly be possible.
I once created application that included it's own source code viewer. I think it's a good alternative to decompilers, which can come with quite dependencies.
I was using NetBeans so packaging the .java files was as easy as changing one filter option. I checked java properties to find the jar file and scanned it just as any zip file for java source files. With this approach having a GUI with JTreeTable populated with source files and JTextArea displaying source code was trivial.
I believe You could do the same with addition of one step more - clipping the source to contain only the selected method. I think it should boil down to simple parser, one that counts opening and closing brackets.
I'm leaving the earlier answer up in case you need it, but JODE hasn't been updated in a long time. Searching around, I can't find any decompiler that is open-source or available in library form.
JODE may be just what you want. The core decompiler is released as a library under the GNU LGPL, so you can integrate it into your program with no issues.

Categories