命令行生成jar包

1.编写java代码,保存为HelloWorld.java

package test;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hello,world");
}
}

2.执行javac HelloWorld.java,生成HelloWorld.class

3.创建mkdir test目录,把HelloWorld.class拷贝到test目录

4. 执行 jar -cef test.HelloWorld HelloWorld.jar test  

以上命令及参数的含义如下:
jar命令为java自带的专用打包工具;
c代表生成新的jar包;
e代表可执行的类,亦即main方法所在的类。书写时要加上包名,在本例中是后面的test.HelloWorld;
f代表生成的jar包的名称,在本例中是HelloWorld.jar。此包名可以随意命名,没有规定;
test最后面的这个参数表示将test目录下的所有文件都打包放到新的jar包中。

5.测试java -jar HelloWorld.jar

输出 hello,world 。

   

猜你喜欢

转载自www.cnblogs.com/yangxuming/p/9263179.html