how to get java source code from war file?

63,810

Solution 1

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

Solution 2

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?

Solution 3

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.

Share:
63,810
Karthik
Author by

Karthik

Updated on March 01, 2022

Comments

  • Karthik
    Karthik about 2 years


    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

    • jgr208
      jgr208 almost 9 years
      extract the war and use a decompiler to try and get the source files for any java classes in the war