您的位置:首页 > 编程语言 > Java开发

命令行编译java程序的三种方法

2014-03-05 23:01 288 查看
manifest文件那一段参考 https://sites.google.com/site/waue0920/Home/java/java-de-da-bao-chengjar-fang-fa

sss@ssstekiMacBook-Pro[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 完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java