Difference Between JAR,WAR and EAR
If there are many files and they are dependent on nature for your program. It is very tough to put them in the classpath to access them run time. For solving this problem java has come up with an archiving concept. Its concept is simple. put all the classes in a zip file and make that available in classpath while running the java code.
Again consider one more example, if our program contains many HTML files, images files, etc. so to download one single file, it is required to download all the components of the file. And for that, we require so many HTTP connections to get all those. It will take more time and patience. so the zip file is a nice concept.
Mainly we have 3 types of zip file in java..
- jar (java archive file)
- war (web archive file)
- ear (enterprise archive file)
jar(Java Archive Tool)
Syntax for jar
jar c|t|x[f] [m] [v] [jar-file] [manifest-file] [files]
jar is a tool that can be used to create and manipulate Java Archive (JAR) files. A JAR file is a compressed ZIP file with an additional ” manifest ” file. The syntax of the jar command is reminiscent of the Unix tar (tape archive) command.
Options to jar are specified as a block of concatenated letters passed as a single argument, rather than as individual command-line arguments. The first letter of this option argument specifies what action jar is to perform and is required. Other option letters are optional. The various file arguments depend on which
option letters are specified.
The first argument to jar is a set of letters that specifies the operation it is to perform. The first letter
specifies the basic action and is required. The three possible values are the following:
- c-Create a new JAR archive. A list of input files and/ or directories must be specified as the final arguments to jar.
- t-List the contents of a JAR archive. If a JAR file is specified with the f option, its contents are listed. Otherwise, the JAR file to be listed is read from standard input.
- x-Extract the contents of a JAR archive. If a JAR file is specified with the f option, its contents are extracted. Otherwise, the JAR file to be extracted is read from standard input. If the command is followed by a list of files and/ or directories, only those files or directories are extracted from the JAR archive. Otherwise, all files in the archive are extracted.
- f-optional command letter. This option indicates that the JAR file to create, list, or extract from is specified on the command line. When f is used with c, t, or x, the JAR filename must be the second command-line argument to the jar (ie, it must follow the block of option letters). If this option is not specified, jar writes the JAR file it creates to standard output or reads a JAR file from standard input.
- m-optional command letter.This option is only used with the c action. It indicates that jar should read the manifest file (partial or complete) specified on the command line and use that file as the basis for the manifest it includes in the JAR file. When this argument is specified after the f option, the manifest filename should follow the destination filename. If m precedes the f option, the manifest filename should precede the destination filename.
- v-optional command letter and it is called Verbose. If this letter is specified with a c action, jar lists each file it adds to the archive with compression statistics. If it is used with a t action, jar lists the size and modification date for each file in the archive, instead of simply listing the filename. If v is used with x. jar displays the name of each file it extracts from the archive.
Example of switches in jar
%jar cvf myJarFile.jar *.java pictures //creates a simple jar %jar tvf myJarFile.jar //lists the contents of the jar %jar xf myJarFile.jar META-INF/MANIFEST.MF //extracts the manifest file from jar %jar cfmv myJarFile.jar manifest.stub test/beans/myFiles//creates a jar with a partial manifest specified
Let’s check out the difference between them: