jar打包最小示例

144 阅读1分钟
  1. HelloWorld.java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}
  1. MANIFEST.MF
Manifest-Version: 1.0
Main-Class: HelloWorld

编译

javac HelloWorld.java

运行

java HelloWorld

情况一:包含MANIFEST.MF,文件中写了Main-Class: HelloWorld

打包

jar cvfm helloworld.jar MANIFEST.MF HelloWorld.class

运行

java -jar helloworld.jar
情况二:不包含MANIFEST.MF,或者未写Main-Class: HelloWorld

打包

jar cvf helloworld.jar HelloWorld.class

运行

java -cp helloworld.jar HelloWorld

使用graalvm生成二进制可执行文件
native-image -jar helloworld.jar -H:Name=helloworld -H:Class=HelloWorld