Hi,
Can you please explain me what is JAR file? How to execute it?
Hi,
Can you please explain me what is JAR file? How to execute it?
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. A JAR file can be created by the command-line jar tool, or by using the java.util.jar API in the Java platform. There is no restriction on the name of a JAR file, it can be any legal file name on a particular platform.
In many cases, JAR files are not just simple archives of java classes files and/or resources. They are used as building blocks for applications and extensions. The META-INF directory, if it exists, is used to store package and extension configuration data, including security, versioning, extension and services.
A JAR (Java ARchive) file is a file that contains the class, image, and sound files for a Java application or applet gathered into a single file and possibly compressed. When a programmer gets a Java program development kit, a small program or utility called "jar" is included. The jar utility lets the programmer create, list, or extract the individual files from a JAR file. In an enterprise, a Java application can be started with a set of JAR files for use during execution. An off-the-shelf open source package can be delivered as a JAR file and run with XML data.
On the Web, a JAR file containing an applet may accompany a Web page. By putting the applet components in a single file and compressing that file, download time is saved. Ordinarily, a browser user will not need to "open" or view a JAR file directly. It is opened when the Web page is received and the applet is in some manner initiated.
The JAR format is based on the popular zip file format and is somewhat comparable to the Unix tar file.
Java Archive (JAR) files are compressed files into which you can store many files. If you place the many classes that your application, or applet need in a JAR file, you reduce the size of your distributabels.
To create a JAR file, go to the directory where your files are located and type:
jar -cf ajarfile.jar *.*
and every file and directory in the current directory will be in a file called ajarfile.jar.
If you like to see what the JAR tility adds to the jar file, type:
jar -cvf ajarfile.jar *.*
The "v" option sends the JAR utility to a verbose mode, printing out information about its activities.
If you want to extract the content of an existing JAR file, type:
jar -xf ajarfile.jar
Again, if you like to see files (or directoies) that the JAR utility extracts from the JAR file, use the "v" option, as in:
jar -xvf ajarfile.jar
If you like see a table of content of a JAR file, type:
jar -tf ajarfile.jar
Again, you can use the "v" option to see some information about the files in the JAR file, as in:
jar -tvf ajarfile.jar
There are other options with the JAR utility, to see them just type:
jar
If you have a UNIX background, you might have realized the definite similariries beteen JAR options and the options of UNIX tar utility.
Creating executable jar files
A new and cool feature of Java2 is the ability to create executable jar files. A jar file is simply a file containing a collection of java files (typically, java .class files, but any other kind of file could be included as well). To make a jar file executable, you need to specify where the "main" Class is in the jar file, so the "java" command knows what main() method to invoke to get the software going.
# First, make sure you have installed Java 1.2 or above. This facility is not available in previous versions of Java.
# Next, create your working java system. In general, you will want to put it into a package. For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. I tested the system to make sure it worked before going on to the next step.
# In the directory in which the psae is located, created a file called "mainClass". This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:
Main-Class: psae.HelloWorld
Note: make sure you type a carriage return after this line; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error.
# Next, I create a jar file called psae.jar using the "jar" command in Java2. I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command:
bertha:~ > jar cmf mainClass psae.jar psae
# Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. Here's the command and its result:
bertha:~ > jar tf psae.jar
META-INF/
META-INF/MANIFEST.MF
psae/
psae/HelloWorld.java
psae/HelloWorld.class
# Having successfully created the jar file, I can now invoke java2 on it with the command line argument:
bertha:~ > java -jar psae.jar Bofinn
Hello World Bofinn
Bookmarks