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

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.mrgr.cn/news/17252.html

相关文章:

  • 龙芯+FreeRTOS+LVGL实战笔记(新)——00关于本专栏
  • 干货分享|分享一款高效的软件卸载神器 Geek Uninstaller
  • 四足机器人控制算法——建模、控制与实践(unitree_guide配置)
  • ubuntu环境快速安装mysql
  • 浅析Java线程池实现原理
  • Java注解和JDK新特性
  • Python爬虫案例四:爬取某个博主的所有文章保存成PDF格式
  • C++11 可变参数模板
  • 两个月冲刺软考——求解关系模式达到了第几范式题型(例题+讲解,一看就会)
  • Linux 进程概念
  • 集成电路学习:什么是GUI图形用户界面
  • 服务器间进行文件传输-SFTPSCP一篇搞定
  • DateTime与时间戳转换
  • 计算机毕业设计推荐-基于python的公司员工考勤管理系统
  • Python编程基础知识,让编程基础更加扎实(输出个人简介)
  • 【解决】CentOS7 生命周期结束后 使用 yum命令报错问题
  • 【面试经验】京东-数据产品面经(一面)
  • 【深海王国】初中生也能玩的建模与3D打印?SolidWorks带你走进3D打印的神奇之旅(1.5)
  • 驱动(RK3588S)第四课时:模块化编程
  • Java-数据结构-ArrayList-练习 ψ(*`ー´)ψ