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
相关推荐
怒放吧德德4 小时前
Python3基础:基础实战巩固,从“会用”到“活用”
后端·python
aiguangyuan4 小时前
基于BERT的中文命名实体识别实战解析
人工智能·python·nlp
喵手4 小时前
Python爬虫实战:知识挖掘机 - 知乎问答与专栏文章的深度分页采集系统(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集知乎问答与专栏文章·采集知乎数据·采集知乎数据存储sqlite
铉铉这波能秀4 小时前
LeetCode Hot100数据结构背景知识之元组(Tuple)Python2026新版
数据结构·python·算法·leetcode·元组·tuple
kali-Myon4 小时前
2025春秋杯网络安全联赛冬季赛-day2
python·安全·web安全·ai·php·pwn·ctf
Olamyh5 小时前
【 超越 ReAct:手搓 Plan-and-Execute (Planner) Agent】
python·ai
deepxuan5 小时前
Day7--python
开发语言·python
曲幽5 小时前
FastAPI不止于API:手把手教你用Jinja2打造动态Web页面
python·fastapi·backend·jinja2·full stack·template engine·web development
禹凕5 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python
Ulyanov5 小时前
基于Pymunk物理引擎的2D坦克对战游戏开发
python·游戏·pygame·pymunk