Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract .war files in java? ZIP vs JAR

Tags:

java

jar

zip

war

I have a web program where I want the user to be able to import a .war file and I can extract certain files out of the .war file. I have found two class libraries: java.util.zip.* and java.util.jar.*. From what I understand, a WAR file is a special JAR file which is a special ZIP file. So would it be better to use java.util.jar? If ZIP and JAR files are pretty much the same why is there a need for two different libraries?

like image 782
user972276 Avatar asked Oct 24 '11 22:10

user972276


People also ask

What is the difference between jar and zip files?

JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. This all means you can open a jar file using the same tools you use to open a zip file.

What is the difference between jar and WAR files in Java?

JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications. The structure of the archives is also different. We can create a JAR with any desired structure.

Are JAR files just zip files?

A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java. util. jar API in the Java platform.


2 Answers

WAR file is just a JAR file, to extract it, just issue following jar command –

jar -xvf yourWARfileName.war 

If the jar command is not found, which sometimes happens in the Windows command prompt, then specify full path i.e. in my case it is,

 c:\java\jdk-1.7.0\bin\jar -xvf my-file.war 
like image 73
iltaf khalid Avatar answered Sep 21 '22 23:09

iltaf khalid


If you look at the JarFile API you'll see that it's a subclass of the ZipFile class.

The jar-specific classes mostly just add jar-specific functionality, like direct support for manifest file attributes and so on.

It's OOP "in action"; since jar files are zip files, the jar classes can use zip functionality and provide additional utility.

like image 38
Dave Newton Avatar answered Sep 22 '22 23:09

Dave Newton