|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
|To invoke an applet packaged as a JAR file
The basic format of the command for creating a JAR file is:
The options and arguments used in this command are:
jar cf jar-file input-file(s)
- The c option indicates that you want to create a JAR file. (c 옵션은 JAR파일을 만드는 것)
- The f option indicates that you want the output to go to a file rather than to stdout. (f 옵션은 stdout 보다 한 개의 파일로 만들기를 원할 때 )
- jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
- The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.
jar command options
||Produces verbose output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.|
||Indicates that you don't want the JAR file to be compressed.|
||Indicates that the default manifest file should not be produced.|
||Used to include manifest information from an existing manifest file. The format for using this option is:
See Modifying a Manifest File for more information about his option.
jar cmf existing-manifest jar-file input-file(s)
Warning: The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
||To change directories during execution of the command. See below for an example.|
jar uf jar-file input-file(s)
In this command:
- The u option indicates that you want to update an existing JAR file.
- The f option indicates that the JAR file to update is specified on the command line.
- jar-file is the existing JAR file that's to be updated.
- input-file(s) is a space-deliminated list of one or more files that you want to add to the Jar file.
Suppose that you want to add the file images/new.gif to the JAR file. You could accomplish that by issuing this command from the parent directory of the images directory:
jar uf TicTacToe.jar images/new.gif
The revised JAR file would have this table of contents:
You can use the -C option to "change directories" during execution of the command. For example:
jar uf TicTacToe.jar -C images new.gif
This command would change to the images directory before adding new.gif to the JAR file. The images directory would not be included in the pathname of new.gif when it's added to the archive, resulting in a table of contents that looks like this: