天天看點

指令行編譯java程式_指令行編譯java程式的三種方法

manifest檔案那一段參考 https://sites.google.com/site/waue0920/Home/java/java-de-da-bao-chengjar-fang-fa

[email protected][src] ll

total 16

drwxr-xr-x  4 sss  staff   136  3  5 17:39 class_helloworld/

drwxr-xr-x  5 sss  staff   170  3  5 17:36 helloworld/

drwxr-xr-x  2 sss  staff    68  3  5 17:59 jar/

helloworld裡存放java檔案。

當 Helloworld.java 是這樣的時候

3 public class Helloworld

4 {

5     public static void main(String[] args)

6     {

7         System.out.println("hello world!");

8     }

9 }

1.目錄轉到helloworld下:

javac Helloworld.java

java Helloworld

2.如果要把class 檔案放到class_helloworld集中管理則:

進入src檔案夾

javac -d class_helloworld/ helloworld/Helloworld.java

java -cp class_helloworld/ Helloworld

3.生成jar再運作(大型程式适用)

javac -d class_helloworld/ helloworld/Helloworld.java

jar -cvf test.jar -C class_helloworld/ .

mv test.jar jar/

chmod +x jar/test.jar

java -cp jar/test.jar Helloworld

--------------------------------------------------------

如果java程式裡面有包

1 package helloworld;

2

3 public class Helloworld

4 {

5     public static void main(String[] args)

6     {

7         System.out.println("hello world!");

8     }

9 }

javac -d class_helloworld/ helloworld/Helloworld.java

jar -cvf test1.jar -C class_helloworld/ .

chmod +x test1.jar

mv test1.jar jar/

java -cp jar/test1.jar helloworld.Helloworld

要檢視jar包的内容 可以輸入 jar -tvf test1.jar

解壓jar包 可以輸入 jar -xvf test1.jar

----------------------------------------------------

接下來試了下怎麼用manifest。

目前目錄為iteratorTest

目錄結構為:

drwxr-xr-x  3 sss  staff  102 Mar  9 18:41 class/

drwxr-xr-x  3 sss  staff  102 Mar  9 18:42 jar/

drwxr-xr-x  4 sss  staff  136 Mar  9 20:00 java/

先 javac  -d class/ java/iteratorTest.java

然後 vim manifest.mf

輸入 (名稱: 值)

Main-class: iteratorTest 注意冒号後面有個空格 否則出錯。從其他資料上看到 這一句是唯一不能省的 其他的都能省。

儲存後執行

jar cvfm test.jar MANIFEST.MF -C class/ .  将class裡的所有.class檔案和manifest.mf一起打包成test.jar

然後

java -jar test.jar 完成。