Contents
Using JAR Files: The Basics
Operation | Command |
---|---|
To create a JAR file | jar cf jar-file input-file(s) |
To view the contents of a JAR file | jar tf jar-file |
To extract the contents of a JAR file | jar xf jar-file |
To extract specific files from a JAR file | jar xf jar-file archived-file(s) |
To run an application packaged as a JAR file (requires the Main-class manifest header) |
java -jar app.jar |
jar Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ jar Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ... Options: -c create new archive -t list table of contents for archive -x extract named (or all) files from archive -u update existing archive -v generate verbose output on standard output -f specify archive file name -m include manifest information from specified manifest file -e specify application entry point for stand-alone application bundled into an executable jar file -0 store only; use no ZIP compression -P preserve leading '/' (absolute path) and ".." (parent directory) components from file names -M do not create a manifest file for the entries -i generate index information for the specified jar files -C change to the specified directory and include the following file |
As usual, the order in which the x
and f
options appear in the command doesn’t matter, but there must not be a space between them.
Caution: When it extracts files, the Jar tool will overwrite any existing files having the same pathname as the extracted files.
Example
Create a jar:
1 |
$ jar cf WordCount.jar WordCount*.class |
Extract a jar:
1 |
jar xf WordCount.jar |