Karthik
Karthik

Reputation: 1352

how to get java source code from war file?


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

Upvotes: 12

Views: 89362

Answers (6)

Ashis Parida
Ashis Parida

Reputation: 11

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.

Upvotes: 1

zeisi
zeisi

Reputation: 5540

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

Upvotes: 18

Arasn
Arasn

Reputation: 158

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

Upvotes: 0

user1400290
user1400290

Reputation: 1750

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

Upvotes: 0

Timo
Timo

Reputation: 3227

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.

Upvotes: 3

Kevin Hooke
Kevin Hooke

Reputation: 2621

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?

Upvotes: 5

Related Questions