Packaging Java Application
Tools >> Built-In | Jar
You have built an application that you want to distribute. You want all the files and folders related to it in one place so that deployment becomes easy without human intervention. Java provides a utility called the jar tool to enable packaging an application for easy distribution and deployment.
The jar tool builds a java archive file that consists of the files and folders of your application. It is similar to the zip compression tool in that it stores the files in a compressed format. It is in fact based on the ZIP file format. Java developers routinely use this utility to compress and store files and folders. But the real purpose of the jar tool is in the ability to package an application for distribution, either for wire transfer or for deployment.
Jar is a cross-platform archiving tool written in Java. In addition to the java class files, it can be used to archive audio and image files. It is also the standard way to package applets for transmission over HTTP because multiple components of an applet such as class files, audio and image files can all be bundled, compressed and wired in a single HTTP connection, instead of opening a new connection for each component. Jars can also be signed so that the client can verify the applet author's credentials before code execution.
Using the Jar file in applet tag in HTML is easy.
<applet code=MyFavoriteApplet.class archive="jars/favorite.jar" width=460 height=160> <param name=dog value="bark"> </applet>
The archive parameter identifies the location of the Jar file on the server. The path information is relative to the HTML file. Multiple Jar files may be loaded by separating them with a comma ",".
Jar file has the extension .jar, and can be opened by WinZip, for example, and provides a lossless compression similar to such tools. It is also possible to execute a Jar file by double-clicking its icon in the Windows environment.
The JDK has the executable jar.exe that you can use to create, and extract the contents of, a jar file. It is present in the bin folder of the install directory. The jar executable has several options to serve various purposes. We shall consider some of the most common uses of this utility tool.
We will begin the subject with a small sub-set of the available command line options of the jar executable and finally end up with an example. The example is a jar package of a trivial application that you can download and execute on your machine. Though the example deals with a trivial application, the essential details of packaging a java application remain the same.