Java_Jar_命令的使用
时间:2010-07-18 来源:fantaxy025025
参考文章:
http://caterpillar.onlyfun.net/Gossip/JavaEssence/SourceClassInJAR.html
http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/ExecutableJAR.htm
补充:
如果在windows下调试,制作manifest 文件需要注意两个地方:
1. 行尾必须有一个回车换行符
2. Main-Class:后加入1个空格然后再加入自己的Main_Class完整名称
否则报错:
D:\workplace\Test>jar cvfm test.jar mymanifest.txt -C %PJ%\bin com
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:393)
at java.util.jar.Manifest.read(Manifest.java:167)
at java.util.jar.Manifest.<init>(Manifest.java:52)
at sun.tools.jar.Main.run(Main.java:123)
at sun.tools.jar.Main.main(Main.java:903)
这个浪费我一个小时,呵呵,以后估计老师会把这个提示加上去了(已经发了email)。
另外附上自己的测试例子:
/*
set PJ=D:\workplace\Test
d:
cd %PJ%
mkdir %PJ%\bin
cd src/com/june/study/main
javac -sourcepath %PJ%\src -d %PJ%\bin Hello.java
cd %PJ%\bin
java com.june.study.main.Hello
cd ..
jar cvf test.jar -C %PJ%\bin com
java -cp test.jar com.june.study.main.Hello
notepad manifest.txt
input:Main-Class: com.june.study.main.Hello
jar cvfm test.jar manifest.txt -C %PJ%\bin com
*/
package com.june.study.main;
import com.june.study.message.Message;
public class Hello {
public static void main(String[] args) {
Message msg1 = new Message("msg1");
Message msg2 = new Message("msg2");
System.out.println("msg1:" + msg1.getMsg());
System.out.println("msg2:" + msg2.getMsg());
}
}
package com.june.study.message;
public class Message {
private String msg;
public Message(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}