A JAR file is an archived file created with the Java Archive Tool. The software used to create JAR files is part of the Java JDK software development kit and is available for most operating systems, including Windows, Linux, Unix and Mac OSX. The JAR utility uses the .zip file format, so JAR files can be extracted with any software that will extract .zip files. JAR files are most commonly used to distribute Java programs and applets.
Basic Information
The JAR utility is used from the command line in both Windows and Unix-based systems (Unix, Linux or BSD). Windows users will open the "Command Prompt" application found in the "Accessories" sub-menu of the main "All Programs" menu. Users on a Unix-based machine will open a terminal window. The terminal window will be found in the operating system's main "Application" menu, under either "System Tools" or "Utilities".
The command to run the JAR utility is "jar". This command comes with many different options, the most common being the following:
c -- create new archive
t -- list the table of contents
x -- extract the archive
u -- update the existing archive
v -- generate verbose output
f -- specify the archive file name
0 -- store the files only; do not compress them
You can use more than one option, but they must follow the JAR command with no white space between the options. Unix users should note that there is no minus (-) sign before the options.
Creating the JAR file
The syntax for creating a JAR file is: jar cvf archive.jar files
There can be any number of files or directories following the archive name and you can use the wildcard (*) symbol to specify multiple files. If a directory is specified as an input file, the directories' contents are added recursively.
For example, if you wanted to add all the text files from a certain directory, issue this command: jar cvf archive.jar *.txt
If you wanted to add the contents of a directory called "Applets," you would issue the command: jar cvf archive.jar Applets
Viewing the Contents
If you want to ensure that all the files are present after creating the jar file, you can view the table of contents with the command: jar tf archive.jar
This command will print a list of all the files within the archive to the screen (or standard output). This will not extract the files or change the archive in any way.
Adding Contents
You can also add contents to an existing jar file with the command: jar uf archiv.jar files
This command will place new files into an existing archive, without changing any other files. It will, however, overwrite any existing files that have the same path name. This is useful when you want to change one file within the archive without having to extract the entire archive.
Extracting the Contents
When you wish to extract the archive, you will use the command: jar xf archive.jar
This will extract the entire contents of the archive into the current directory. If there are any directories in the archive it will create the same directory structure within the current directory. You can also specify that you only want certain files to be extracted by adding the names of the files at the end of the command in the following way: jar xf archive.jar file1 file2 file3
When extracting the files, the original jar file remains unchanged.
Tags: command archive, This command, will extract, archive This, archive will, archive without, command archive This