I have one War files (a Java application) consist of 64 class file along with image/CSS/JS files. I have lost the source of this war file. Now I want to do some changes in code. I am looking for some expert advice from community on following questions.
Is there best way to convert .class files to .java file without losing any section of code?
What are the tools if any for this task?
What are challenges/drawbacks of converting .class files to .java files to recreate war file?
If you do not want to spend horrible times in understanding decompiled bytecode, please start using a version control system and a remote repository for any project of more than two classes.
In particular, you can use Git with GitHub, open source are hosted for free while if you want to protect your code you can get a commercial subscription which is cheap anyway.
For this time, help yourself with a java decompiler such as JD:
Use a java decompiler to decompile the bytecode.
Create a git repository out of the decompiled java classes
Constantly synchronize it with the remote repository
Conversion to Java Bytecode is almost completely reversible. As #StephenC said, In the regenerated sources, you will lose comments, names of local variables etc. It might not compile right away but you should be able to get it to compile again, with a little modification.
You need a Java Decompiler to do that. Try JD-GUI or DJ Java Decompiler
In case you obfuscated your sources, the sources you recover will not be harder to understand and reverse engineer. Otherwise, with just a little tinkering, you should be able to get back your sources.
Related
I'm new to Java and I'm trying to understand something. I managed to get a .jar file for a local device to communicate with my laptop. It works fine, but I would like to do an upgrade and scoop around the codes and change some details. I used a Java Decompiler on the .jar file and managed to take a look at the class files. However, whenever I decompile them into source codes, errors will occur in my IDE, and I'm unable to build them. What exactly happened? If it's working fine in the .jar why does it produce errors?
Compilation is a lossy process, so decompilation is always a "best effort" endeavor. Java bytecode is unusually high level, so Java decompilation is relatively easy compared to other languages, but it will still never be 100% complete. Therefore, you should not be surprised to see errors when re-compiling decompiled source.
If you want to work directly with bytecode, I would recommend using the Krakatau disassembler and assembler. Instead of decompiling to Java source, the disassembler converts bytecode into a human readable assembly format which is specifically designed to be converted to and from bytecode losslessly. However, it requires a good understanding of low level Java bytecode to use.
Completely agree with Antimony. There is no decompiler which decompiles always without any error. Sometimes it decompiles well, sometimes not.
The best way is to have several decompilers and use them each by other to decrease errors in decompiled source.
If you use Eclipse I would suggest https://marketplace.eclipse.org/content/enhanced-class-decompiler
If you use IntelliJ try this decompiler Jar Explorer.
I made a big error and I need your help:
I work on a project for the school with java and I made a big error!
I sent to me only the *.class files and deleted the other files(I work in my school with VM).. Is there a way to compile these .class files to .java files?
Thanks
You need to use decompiler for that not the compiler.
First decompile using decompiler and then do some changes what ever you needed and then again compile it.Than your problem will solve
SEE HERE
Try the Java Decompiler. It should be able to reverse your .class files.
You can also try Show my code if you don't mind submitting your .class files online.
You should be aware that different decompilers may give different results, so trying different ones may be helpful. See Choose and test java decompiler
The utility you need is called java decompiler. There are several available.
Try JAD from command line. You can also install plugin for eclipse.
And BTW if you are on windows is it possible that your source files are in recycle bin? Or if you are using eclipse try to check the eclipse local history (right click on your project and choose "Restore from local history")
You need to decompile your class file. But, after decompile you also need some necessary changes to get your original java file.
Try this.
I preper using JD-GUI. You can download from link http://java.decompiler.free.fr/?q=jdgui
Dont Worry download DJ Java Decompiler 3.7 download only this version.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to unzip a jar library that I am using check the classes if it's core functionality is updated to what is presented on GitHub.
How do you unzip a .jar file?
Update:
You wont be able to get class .txt without the source code attached to them if you unzip the directly.
Thanks, to alegen's method of using a Java Decompiler you can decompile classes.
Open the JAR file with WinRAR but unless the people also packaged the source code, you will only find .class files which contain bytecode. You will have to also use a decompiler; I usually use JD Java Decompiler.
You can extract files from JAR file using a variety of tools, including plain ZIP file tools ... and of course the jar command that comes as part of the JDK.
It sounds like your real problem is that you want to decompile the .class files in a JAR file so that you can compare them with some Java source code.
Well, the bad news is that it won't work ... especially in the large-scale. Yes, there are decompilers that will (on a good day) produce something that resembles Java. However:
The decompiled Java won't look anything like the original source code that were used to produce the .class files. Comparing the decompiled files with the original source code (or something based on the original source code) is going to be a tedious and difficult MANUAL process.
There is a fair chance that the decompiled code won't even be valid (compilable) Java.
A better approach might be to compile the sourcecode on GitHub and try to compare the two versions of the .class files. However, even that isn't straight forward because:
The .class files contain timestamps, and other stuff that depend on build paths, compilation switches, and so on. You have to figure out how to remove this "noise" when comparing the .class files.
Different versions of the Java compiler produce different flavours of classfile.
Even when the classfile version numbers are the same, there can be compiler-version-specific differences in the generated bytecodes.
For small-scale comparisons, decompilation might do ... provided you are prepared to work at the problem. You could also use javap to print the bytecodes in the class files and compare the javap outputs.
So what you want do do is to check if two jars are the same, to ascertain that the methods on github are the same as the ones in your jar?
I think you're trying to solve the wrong problem here. What you should do is to just check out the code from github and build it yourself. There should be a file called README, INSTALL or something similar in the root directory of the project.
Now, of course, you might not want the newest version of the project, you might want the latest stable release or whatnot, which is perfectly doable: Just download the release you want from git, and build yourself. You now have both the code, and a jar.
If you do this, you are ensured that the code matches the jar, and no icky decompilation is needed, just a quick make or ant later, as specified by the readme file.
first i'd like to point out that im fairly new to netbeans and java, couldnt exactly find what i need on the web.
I downloaded a software's source files and was modifying it on netbeans. naturally when building the project, netbeans creates in (for example) documents/project_folder/dist/ a lib folder and the finished JAR file that i can use.
if i delete by mistake the source files, and would like to apply all the changes i previously made to fresh source files using the JAR and lib folder i still have, is it possible and how?
First of all it is not possible to get a source again from jar file in lib folder. Jar file is executable file which will just contain class files. You can use java decompilers to get source again.
NB do not has a version control nor auto save by default. You may have a look on git in NB7.
When you restore from Jar with decompiler there is a big chance to get the source back, even more if you know what your code is all about.
You can get the source back by using some decompiler. There are many decompilers available like DJ Decomipler. My favourite is Jd Java Decompiler. Its very small in size and does its job pretty well
I'm rather an Eclipse user but I guess both Eclipse and Netbeans are similar to a certain extent. Unless the jar has been created including the source, you won't have other choices that decompile the class files.
When needed, I usually go with JD-GUI which does a pretty good job decompiling jars.
It is not possible to retrieve exact source code from a compiled byte code. But there is a possibility to recover your code to some extent
JD is a good Java decompiler.
http://java.decompiler.free.fr/?q=jdgui
I think JD is a super hero in cases that the original source code is lost.
Keep your source code on any SCM. Use remote SCM not to loose your code. See: google code, github, launchpad etc.
I recently lost my netbeans project folder of the project that I was working on at the time. However somewhere on a server here at the company that I work at, I deployed it. So that means that I (hopefully) can still get hold of the .war file.
Is it possible to then 'unpack' this .war and get my .java source files back that I was working on at the time?
Thanks in advance!
If the .java sources aren't in the WAR (and they should not be), you'll have to take the additional step of decompiling them using a tool like JAD.
This is a good time to set up a version control system, like Subversion or Git or Mercurial, so this never happens to you again. Get into the habit of using source code management, even if you're the sole developer working on the project.
Short answer: No.
Slightly longer answer: Look for Java decompilers, but they won't give you your Netbeans project folder.
You only get *.class files from your war (rename war to zip and use a decompression tool).
Then you could decompile them.
See this related question for some suggestions.
Suppose if you had exported the source files while creating the war, you can get it. Else, JAD is your only hope that too cannot fully rely on it.
It is possible to unzip the war file where you will get only class files and other property files. Then use Java decompiler to see source code and it works really well (not recommended).
Also you can change the property files without JD and all you need to do just change the property files and zip to war file again.It will work.
But I would recommend you to maintain source code in SVN or TFS or multiple copies with version numbers in local system at any point of time.
In Eclipse, you can Import your War, it will create a new Project and set the resources in the project structure. IF your war holds the source java files, the project will cointain your sources. IF not, the packages will be empty and you will have to manually decompile your classes from bytecode to java files, using jad or another decompiler.