Some Java Guy
Some Java Guy

Reputation: 5118

Convert .jar to .war

How do I convert .jar to .war file format.

Basically I want to read the file.jar. But when I import in eclipse, it obviously imports as .class files. I want to read in .java format and read the code.

Kindly help

Upvotes: 2

Views: 17607

Answers (4)

impropositum
impropositum

Reputation: 141

Your statement implies that your file.jar doesn't contain Java Code.

Here I suggest to you.

  1. Differ .jar and .war file format. It is already on previous answer.

  2. If you want to read the code from your file.jar, actually you can decompile it first. There are many Java decompiler that is easy to use. I suggest you use JD for this purpose.

Upvotes: 1

Jesper
Jesper

Reputation: 206786

If the reason you are trying to convert a jar to a war is only because you want to be able to see the source code of the classes in the jar, then trying to convert it to a war is not the right thing to do.

A Java jar (Java ARchive) file is just like a zip file, an archive that can contain other files. Normally it will contain class files (compiled Java classes). A war (Web ARchive) is the same as a jar file, but with a certain prescribed directory structure that makes it suitable for deployment on a Java EE application server. Converting a jar file to a war file will not automatically make it possible to see the source code of the classes in Eclipse.

To see the sources in Eclipse, you'll need to download those sources separately. For many open source projects, you can get the sources in a separate jar. For example, for the library somelib.jar you might find somelib-sources.jar or something similar from the website where you got the library.

Then, in Eclipse, right-click the jar file that's attached to your project as a dependency and select Properties from the popup menu. Then go to "Java Source Attachment". Enter the location of the ...-sources.jar or a directory that contains the source code there and click OK.

Upvotes: 1

Miquel
Miquel

Reputation: 15675

As @Santosh says, war and jar are the same compression (so you can rename it to jar and use jar -x to uncompress it) but the internal organization is different. It seems to me like you just want to fish out the java files, so uncompressing would suffice...

mv file.war file.jar
jar xf file.jar

Upvotes: 1

Santosh
Santosh

Reputation: 17893

Please remember that .jar and .war are different file formats. Even though, they are zipped set of files, a war file has a standard organization structure . Where as a jar file is simple a standard zip archive of files generally used as packaged binary archive. What you are trying to do is difficult to achieve and there are no straight forward way to do that. You will have to do it manually.

Upvotes: 1

Related Questions