how to get java source code from war file?

82.6k views Asked by At


I do not have the latest source code but have the war(up to date) file deployed on server.

Please suggest the best ways to
1) Retrieve source code from war/ear
2) Compare & Merge/update the available source code with the code present in war/ear but missing in available source code(I am using ECLIPSE IDE)

Thanks in advance

6

There are 6 answers

0
zeisi On BEST ANSWER

War files are basically zip files, so they are easy to extract. (using unzip or just renaming the file) Next you could use a Java decompiler like JD. But you won't get the original Java code as the compiler does a lot of optimization. But it should give you a good starting point

0
Kevin Hooke On

Once you've extracted the classes from the EAR/WAR/Jars, use JAD to decompile the code you're interested in to get back to the source: http://varaneckas.com/jad/

I'm not sure there's any out-of-the-box tool that is going to compare/diff your original source with the decompiled source produced from something like JAD though. Also bear in mind, decompiling classes back to source is not going to produce source that looks identical to the original source - code style is going to be different, maybe even some structure of the code. It's going to be difficult to do a diff between the original source and decompiled source.

If you have the original source but not the source for the code that is currently deployed, maybe a better question is to ask 'why not'? If there's something missing in your build process where you are not tracking what source is being used for each build, maybe this is an easier issue to address moving forward, rather than trying to do something clumsy and error prone like a diff between some other source and decompiled source?

1
Ashis Parida On

Here is your complete solution.

  1. If while creating war file, you have to make sure that you have added the code.
  2. Otherwise, do one thing.

Deploy the war file in your server, may be on tomcat server.

To deploy the war file, you need to put that war file in webapps folder (C:\ASHIS_CODE\apache-tomcat-9.0.65\webapps). enter image description here After putting, you need to restart your tomcat server. Then one folder with same name as of your war file name, will be created in side webapps folder.

Open that folder in your eclipse or any other ide, that folder contains your project code.

** Hope this clears your issue.

0
Timo On

The exact answer: it is not possible to get the original source code (.java files) from a war as opposed to a jar (java archive). When you create a jar file, you can decide if you want to include the .java files. Only a java decompiler can help, see the other answers.

0
user1400290 On

Using JD GUI you can the source code with java code, but you'll need to

0
Arasn On

Inside the war folder, under specific module - Based on your project hierarchy (if maven project -these config will be available in Pom.xml - it will define which path and what jar name) you will have the Core JAR files of each module.

Open those jar files using any decompiler , you will be able to find the class/java files in it..