当前位置: 首页 > article >正文

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:

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:

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:

java -jar JAR-name

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

An Example

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

Manifest-Version: 1.0
Main-Class: Test

Test.java

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

build_jar.bat

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:

jar cfe app.jar MyApp MyApp.class

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

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:

jar cfe Main.jar foo.Main foo/Main.class
An Example

HelloWorld.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

http://www.kler.cn/a/286612.html

相关文章:

  • 在 DevOps 实践中,如何构建自动化的持续集成和持续交付(CI/CD)管道,以提高开发和测试效率?
  • 【Rust自学】16.3. 共享状态的并发
  • CVE-2023-38831 漏洞复现:win10 压缩包挂马攻击剖析
  • MySQL 基础学习(3):排序查询和条件查询
  • LeetCode 0045.跳跃游戏 II:贪心(柳暗花明又一村)
  • Couchbase UI: Indexes
  • SpringBoot 引入使用消息队列RabbitMQ通信 配置连接 无路由模式
  • STM32 使用8720 通过LWIP发送数据
  • k8s helm
  • 【MySQL】字节跳动MySQL连环40问(网图)
  • WHAT - 通过 react-use 源码学习 React(UI 篇)
  • LabVIEW布尔值比较与信号状态上升沿下降沿检测
  • jsmn输出
  • using showdown js with openAi streaming response
  • 详解CSS
  • Jenkins Environment Injector Plugin 插件详解
  • C语言 | Leetcode C语言题解之第390题消除游戏
  • Vue项目“npm run serve”总卡住的问题 已解决
  • 深度强化学习算法(七)(附带MATLAB程序)
  • 鸿蒙Harmony开发实战:自定义圆形组件-Canvas
  • NLP从零开始------文本中阶序列处理之语言模型(完整版)
  • 基于机器学习的酒店评论分析与推荐系统设计
  • JavaWeb实战教程:如何打造旅行社网站系统,提升在线服务能力?
  • Ubuntu 搭建 GLFW 环境及其相关测试 demo
  • UE开发中的设计模式(四) —— 组合模式
  • 使用自制COCO数据集进行PaddleDetection模型训练