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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值