learn more...The basic format of the command for creating a JAR file is: jar cf jar-file input-file(s) The options and arguments used in this command are:
The c and f options can appear in either order, but there must not be any space between them. This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive. Note The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8. You can add any of the additional options to the cf options of the basic command shown in the table below:
An ExampleLet us look at an example, a simple TicTacToe applet. This demo contains a bytecode class file, audio files, and images having the structure below: TicTacToe The audio and images subdirectories contain sound files and GIF images used by the applet. You can obtain all these files from jar/examples directory. To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:
jar cvf TicTacToe.jar TicTacToe.class audio images The audio and images arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file TicTacToe.jar will be placed in the current directory. Because the command used the v option for verbose output, you would see something similar to this output when you run the command: adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%) You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like: jar cvf0 TicTacToe.jar TicTacToe.class audio images You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there is a tradeoff in that download time over a network may be longer for larger, uncompressed files. The Jar tool will accept arguments that use the wildcard * symbol. As long as there weren't any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file: jar cvf TicTacToe.jar * Though the verbose output doesn't indicate it, the Jar tool automatically adds a manifest file to the JAR archive with path name META-INF/MANIFEST.MF. In the previous example, the files in the archive retained their relative path names and directory structure. The Jar tool provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It's modeled after TAR's -C option. As an example, suppose you wanted to put audio files and gif images used by the TicTacToe demo into a JAR file, and that you wanted all the files to be on the top level, with no directory hierarchy. You could accomplish that by issuing this command from the parent directory of the images and audio directories: jar cf ImageAudio.jar -C images . -C audio . The -C images part of this command directs the Jar tool to go to the images directory, and the . following -C images directs the Jar tool to archive all the contents of that directory. The -C audio . part of the command then does the same with the audio directory. The resulting JAR file would have this table of contents: META-INF/MANIFEST.MF By contrast, suppose that you used a command that did not employ the -C option: jar cf ImageAudio.jar images audio The resulting JAR file would have this table of contents: META-INF/MANIFEST.MF |
||||||||||||||||||
Disclaimer
1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. link to this article |