5

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!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 9
    ... source control ... – JRL Oct 05 '11 at 13:37
  • It's easy to unpack the WAR file - it is just a Zip file with a specific layout. On Mac or Linux, you can just call "unzip" on the file. Unfortunately, that doesn't get you the source files, just the compiled .class files, so you'll need a decompiler to be able to read java code, and it won't be the same as your original source code. – Thomas Andrews Oct 05 '11 at 13:38
  • 3
    You mean version control? Hell yes, a valuable lesson I now learned early in my career. –  Oct 05 '11 at 13:44

6 Answers6

9

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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Ah that sucks. What would the source code look like after i de-compiled it? messed up or would it still be nice indented etc? –  Oct 05 '11 at 13:37
  • Probably not what you want. I can't say. Try it with one and see. – duffymo Oct 05 '11 at 13:43
  • I have decompiled osurce before, it came out fin (think I used JAD also). I also have the habit of packing the source with dev war file when depolying to non-production server (e.g staging/development servers) along with using VCS. I can't tell you how many times that has saved me. – Dark Star1 Sep 10 '12 at 12:30
  • Wrong idea, in my opinion, even if it "worked". That's a terrible habit. I'll say it again: version control is a must. You aren't a professional programmer if you aren't using it. – duffymo Sep 10 '12 at 12:58
  • @duffymo what are the downsides of having the source in the WAR file? – Shurmajee May 08 '14 at 10:27
  • Useless; they aren't used by the app server. You might not want to give your source to everyone who uses your WAR. Bigger WAR takes up more room on server disk (small concern these days). Almost two years later, and I'd still say your source belongs in a source code management system like Git, not in a WAR file. – duffymo May 08 '14 at 12:03
2

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.

Community
  • 1
  • 1
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
2

Short answer: No.

Slightly longer answer: Look for Java decompilers, but they won't give you your Netbeans project folder.

michael667
  • 3,241
  • 24
  • 32
  • Okay so the following should work: 1 - I unpack the .war file, 2 - I de-compile the class files, 3 - I re-create the netbeans project from existing source? Sounds about right, right? –  Oct 05 '11 at 13:45
0

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.

Sreekumar
  • 37
  • 1
  • 9
0

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.

Don G.
  • 117
  • 6
0

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.

rozar
  • 1,058
  • 3
  • 15
  • 28