jar命令
jar 是java的一个压缩文件名 (java archive),但是格式仍是zip的,所以你可以用winzip等支持zip格式的软件打开,如果你是想自己生成一个jar文件,你可以用 jar工具来做。
官方文档: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
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
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)
.
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
We want to execute the main
method in the class MyClass
in the package MyPackage
when we run the JAR file.
We first create a text file named Manifest.txt
with the following contents:
Main-Class: MyPackage.MyClass
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.
We then create a JAR file named MyJar.jar
by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClass
When you run the JAR file with the following command, the main
method of MyClass
executes:
java -jar MyJar.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
----------------------------------------------------------------------------------------------
在命令行打 jar
即可看到它的帮助:
用法:jar {ctxu}[vfm0M] [jar-文件] [manifest-文件] [-C 目录] 文件名 ...
选项:
-c 创建新的归档
-t 列出归档内容的列表
-x 展开归档中的命名的(或所有的〕文件
-u 更新已存在的归档
-v 生成详细输出到标准输出上
-f 指定归档文件名
-m 包含来自指定的清单(manifest〕文件的清单(manifest〕信息
-0 只存储方式;未用ZIP压缩格式
-M 不产生所有项的清单(manifest〕文件
-i 为指定的jar文件产生索引信息
-C 改变到指定的目录,并且包含下列文件:
如果一个文件名是一个目录,它将被递归处理。
清单(manifest〕文件名和归档文件名都需要被指定,按'm' 和 'f'标志指定的相同顺序。
示例1:将两个class文件归档到一个名为 'classes.jar' 的归档文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:用一个存在的清单(manifest〕文件 'mymanifest' 将 foo/ 目录下的所有
文件归档到一个名为 'classes.jar' 的归档文件中:
jar cvfm classes.jar mymanifest -C foo/ .
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现