How to run a JAR file

一、Setting an Application's Entry Point

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

text 复制代码
Main-Class: classname

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

The value classname is the name of the class that is your application's entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

We then create a JAR file named MyJar.jar by entering the following command:

shell 复制代码
jar cfm MyJar.jar Manifest.txt MyPackage/*.class

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

shell 复制代码
java -jar JAR-name

The main method of the class specified in the Main-Class header is executed.

An Example

MANIFEST.MF
注意:最后一行必须为空行

text 复制代码
Manifest-Version: 1.0
Main-Class: Test

Test.java

java 复制代码
public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}

build_jar.bat

text 复制代码
javac Test.java
jar cfm test.jar MANIFEST.MF Test.class
del Test.class
java -jar test.jar
del test.jar

二、Setting an Entry Point with the JAR Tool

The 'e' flag (for 'entrypoint') creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.

For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

shell 复制代码
jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

shell 复制代码
java -jar app.jar

If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

shell 复制代码
jar cfe Main.jar foo.Main foo/Main.class
An Example

HelloWorld.java

java 复制代码
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

build_jar.bat

javac HelloWorld.java
jar cvfe HelloWorld.jar HelloWorld HelloWorld.class
:: jar cvfe HelloWorld.jar HelloWorld *.class
java -jar HelloWorld.jar
::java -cp HelloWorld.jar HelloWorld
::java -jar HelloWorld.jar -classpath HelloWorld
del HelloWorld.class
del HelloWorld.jar

Reference

  1. How to run a JAR file
  2. Setting an Application's Entry Point
相关推荐
紫雾凌寒1 小时前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
令狐少侠20111 小时前
Idea 和 Pycharm 快捷键
pycharm·intellij-idea·idea
程序员杰哥2 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋11 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户11 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis