JAR 和Zip一樣,都是將許多檔案置放在單一壓縮檔內.所不同之處是與java一樣具有跨平台特性,除了class檔,還可以將音訊(audio)和影像(image)置於其中.
用法:jar {ctxui}[vfm0Me] [jar 檔案] [清單檔案] [進入點] [
選項:
-c 建立新的歸檔
-t 列出歸檔的目錄
-x 從歸檔中擷取已命名的 (或所有) 檔案
-u 更新現有歸檔
-v 在標準輸出中產生詳細輸出
-f 指定歸檔檔案名稱
-m 包含指定清單檔案中的清單資訊
-e 為獨立應用程式指定應用程式進入點
已隨附於可執行 jar 檔案中
-0 僅儲存;不使用 ZIP 壓縮方式
-M 不為項目建立清單檔案
-i 為指定的 jar 檔案產生索引資訊
-C 變更至指定目錄並包含後面所列的檔案
如果有任何檔案是目錄,則會對其進行遞迴處理。
清單檔案名稱、歸檔檔案名稱和進入點名稱
的指定順序與指定 'm' 旗標、'f' 旗標和 'e' 旗標的順序相同。
範例 1:將兩個類別檔案歸檔至名為 classes.jar 的歸檔中:
jar cvf classes.jar Foo.class Bar.class
範例 2:使用現有清單檔案 'mymanifest' 並將
foo/ 目錄中的所有檔案歸檔至 'classes.jar' 中:
jar cvfm classes.jar mymanifest -C foo/ .
Compress Files To A Java Archive (JAR)
This is by far the most common use for JAR files: to compress multiple files into a single JAR archive. JAR files can be opened with WinZip or WinRar. In terms of Java applications, the ability to archive any number of source or class files into one single archive represents the biggest advantage - distributing one file containing hundreds of files is so much easier than distributing hundreds of files separately!
The jar utility program is run from the command line (DOS prompt or bash for example, depending on your OS). Here is how to create a compressed JAR file:
jar cf archive_name.jar files
Let's look at each part of that command line.
jar
The command to run the jar utility.
CF
Create a new archive with the file name specified. These two options are from this list of common 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
- 0 store only; use no ZIP compression
- 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 fileMultiple options can be used together. They all must appear after the "jar" command with no white space separating them.
archive_name.jar
Name of the JAR file. This can only be included if you use the 'f' option.
files
Names of all the files you want to put in the jar file. This could be just one name or a list of multiple names separated by space. Names can use pattern matching characters to match multiple files.
設定程序入口class
1. create a temp file with extension .mf and add the following lines:(suppose the maninfest file name mymanifest.mf)
Manifest-Version: 1.0
Main-Class: test1
2. jar cvfm classes.jar mymanifest.mf path/ .class
Running an Executable jar file from command line
java -jar classes.jar
Running An Executable jar from explorer(windows)
Within Windows, it is also possible to set up Windows Explorer so that you can double click on the JAR file icon to execute the file (handy for GUI applications). The first thing you must do is set up the correct association with the 'javaw.exe' application that JDK for Windows will have. Click here for an older example with pictures! Open Windows Explorer and select Tools | Folder Options | File Types.
If there is no JAR file type, create it. Give it a description like
jar - Executable Jar File
to ensure it sorts under 'jar'. Create or edit the action called "open" and associate it with the following action:
"C:\Java\jdk1.4.0\bin\javaw.exe" -jar "%1"
Of course, you will replace "C:\Java\jdk1.4.0\bin\javaw.exe" with whatever path is correct on your machine.
IMPORTANT: include the double quotes to take care of names with spaces.If you are using something other than Windows and you know how to set up an association in your OS, please contact me.