java 打jar包 轉

來源:互聯網
上載者:User

jar 應用 先開啟命令提示字元(win2000或在運行筐裡執行cmd命令,win98為DOS提示符),輸入jar -help,然後斷行符號(如果你盤上已經有了jdk1.1或以上版本),看到什麼:
用法:jar {ctxu}[vfm0Mi] [jar-檔案] [manifest-檔案] [-C 目錄] 檔案名稱 ...

 

選項:
-c 建立新的存檔
-t 列出存檔內容的列表
-x 展開存檔中的命名的(或所有的〕檔案
-u 更新已存在的存檔
-v 產生詳細輸出到標準輸出上
-f 指定封存檔案名
-m 包含來自標明檔案的標明資訊
-0 只儲存方式;未用ZIP壓縮格式
-M 不產生所有項的清單(manifest〕檔案
-i 為指定的jar檔案產生索引資訊
-C 改變到指定的目錄,並且包含下列檔案:

如果一個檔案名稱是一個目錄,它將被遞迴處理。
清單(manifest〕檔案名稱和封存檔案名都需要被指定,按'm' 和 'f'標誌指定的相同順序.

樣本1:將兩個class檔案存檔到一個名為 'classes.jar' 的封存檔案中:
jar cvf classes.jar Foo.class Bar.class

樣本2:用一個存在的清單(manifest)檔案 'mymanifest' 將 foo/ 目錄下的所有檔案存檔到一個名為 'classes.jar' 的封存檔案中:
jar cvfm classes.jar mymanifest -C foo/ .

來個小例子試試看:
我們只有一個HelloWorld,如下:
public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hi, Hello World!");
    }
}
我將這個java檔案存到C盤跟目錄下,ok,接下來,

在先前開啟的命令提示字元下(跳轉到C盤提示符下),我們輸入javac HelloWorld.java,然後繼續輸入:jar cvf hello.jar HelloWorld.class,斷行符號後去你的C盤看看,多了什麼,沒錯 hello.jar 。

基本的步驟我們現在都知道了,你可以自己去嘗試一下隨著jar後面的參數的不同,結果有什麼變化。

緊接著我們看看如何運行我們的jar包。

在進入正題之前,你要先開啟我們剛剛做好的jar包看看,多了什麼呢,META-INF目錄?再看看裡面是什麼,還有一個MANIFEST.MF檔案是不是?用文字編輯器(我這裡是UltraEdit)開啟它看看:
Manifest-Version: 1.0
Created-By: 1.4.2 (Sun Microsystems Inc.)

就是這樣。這裡我們對它進行修改,加一句:Main-Class: HelloWorld (在第三行)。這個就是我們之前寫的那個類,也就是我們的入口類。也即,
Manifest-Version: 1.0
Created-By: 1.4.2 (Sun Microsystems Inc.)
Main-Class: HelloWorld

接下來,我們在命令提示字元裡執行:
jar umf MANIFEST.MF app.jar

這樣我們使用了我們自己的MANIFEST.MF檔案對原來預設的進行了更新。你不妨可以再進去看看是不是添上了Main-Class: HelloWorld這一句。

Ok,這個最後的一步了,來驗證我們做的一切,在命令提示字元中輸入:
java -jar hello.jar(執行)

出現了什麼,――Hi, Hello World!
我們再來看看jar檔案在tomcat中發布,注意:在tomcat中我們就不能再用jar這種格式,而改war格式,它是專門用於web應用的,其實整個過程下來基本上和jar是類似的:

先準備我們要打包的資源。

找到存放tomcat的webapps目錄,進到其中,建立一個檔案夾,這裡命名為hello,再進去建立WEB-INF檔案夾,再進去建立 classes檔案夾,此時我們也將我們唯一的servlet,HelloWorld.java放到這裡,在與classes目錄同級下建立一檔案 web.xml。Ok,目前我們初步建立了一個簡單的web應用。

在命令提示字元下進到先前創製的hello目錄下,執行 jar cvf hello.war * ,我們便得到hello.war。將它拷貝至webapps目錄下,ok,來看最後一步,開啟tomcat的目錄conf中的server.xml,加入:
reloadable="true"/>
大功告成!運行它,啟動tomcat,後在瀏覽器中輸入http://localhost:8080/hello/HelloWorld,有了嗎?

############

jar基本操作:

############

1. 建立jar檔案
jar cf jar-file input-file(s)
c---want to Create a JAR file.
f---want the output to go to a file rather than to stdout.
eg: 1)jar cf myjar.jar query_maintain_insert.htm
2)jar cvf myjar.jar query_maintain_insert.htm
v---Produces verbose(詳細的) output.
3)jar cvf myjar.jar query_maintain_insert.htm mydirectory
4)jar cv0f myjar.jar query_maintain_insert.htm mydirectory
0---don't want the JAR file to be compressed.
5)jar cmf MANIFEST.MF myjar.jar yahh.txt
m---Used to include manifest information from an existing manifest file.
6)jar cMf MANIFEST.MF myjar.jar yahh.txt
M---the default manifest file should not be produced.
7)jar cvf myjar.jar *
*---create all contents in current directory.
2. 察看jar檔案
jar tf jar-file
t---want to view the Table of contents of the JAR file.
eg: 1)jar vft yahh.jar
v---Produces verbose(詳細的) output.
3. 提取jar檔案
jar xf jar-file [archived-file(s)]
x---want to extract files from the JAR archive.
eg: 1)jar xf yahh.jar yahh.txt(僅提取檔案yahh.txt)
2)jar xf yahh.jar alex/yahhalex.txt(僅提取目錄alex下的檔案yahhalex.txt)
3)jar xf yahh.jar(提取該jar包中的所有檔案或目錄)
4. 修改Manifest檔案
jar cmf manifest-addition jar-file input-file(s)
m---Used to include manifest information from an existing manifest file.
5. 更新jar檔案
jar uf jar-file input-file(s)
u---want to update an existing JAR file.

-->產生exe:<--

第一種:在jbuilder中:     
首先你要保證Run菜單-->Run    Project能順利運行     
然後Wizards菜單-->Native    Executable    Builder     
選中Compress    the    contents    of    the    archive(產生jar檔案的話)     
Next-->Next-->選中Always    include    all    classes    and    resources再Next-->Next-->Next     
選中Windows    GUI"exe"(產生EXE檔案的話)-->Finish     
再在項目的檔案清單中的Native    Executable右擊-->Make就可以了     
  
第二種:在cmd    下產生jar檔案     
abc.txt內容如下:     
Manifest-Version:    1.0         
Main-Class:    main-class-name(回車)     
在cmd下:         
javac    *.java     
jar    cvfm    abc.jar    abc.txt    *.class     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.