jar Manifest例子如何将Manifest文件添加到jar文件中

・3 分钟阅读

摘要:如何将Manifest文件添加到Java jar文件中。

我刚刚为Java客户端应用程序创建了一个可执行jar文件,

jar cfm curtain.jar Manifest.txt com/

用户可以非常简单地运行你的客户端应用程序,如下所示:

java -jar curtain.jar

但是,如果不包括Manifest文件,用户尝试运行应用程序,就会得到错误消息:

Failed to load Main-Class manifest attribute from curtain.jar

FWIW,我的清单文件Manifest.txt中只有一行,告诉Java解释器在哪里可以运行应用程序,如下所示:

Main-Class: com.devdaily.curtaindemo.CurtainDemoMain

你可以在命令行上输入jar来查看所有的jar命令选项,

Usage: jar {ctxu}[vfm0Mi] [jar-file] [manifest-file] [-C dir] files ...
Options:
 -c create new archive
 -t list table of contents for archive
 -x extract named (or all) files from archive
 -u update existing archive
 -v generate verbose output on standard output
 -f specify archive file name
 -m include manifest information from specified manifest file
 -0 store only; use no ZIP compression
 -M do not create a manifest file for the entries
 -i generate index information for the specified jar files
 -C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name and the archive file name needs to be specified
in the same order the 'm' and 'f' flags are specified.
Example 1: to archive two class files into an archive called classes.jar: 
 jar cvf classes.jar Foo.class Bar.class 
Example 2: use an existing manifest file 'mymanifest' and archive all the files in the foo/ directory into 'classes.jar': 
 jar cvfm classes.jar mymanifest -C foo/ .

从最后一行可以看出,在Jar文件手动方式包含Java manifest :

jar cvfm classes.jar mymanifest -C foo/ .
Xn_warm profile image