Convert .jar to .war

15,944

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
15,944
Some Java Guy
Author by

Some Java Guy

I did Java/J2EE for long time, now AWS architecture.

Updated on June 04, 2022

Comments

  • Some Java Guy
    Some Java Guy almost 2 years

    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