java打ext包命令_jar命令打jar包

目录:

1. 准备工作

2. jar命令打jar包

1) jar命令用法

2)第一种方式 jar -cvf ./class2json.jar ./com  (这种方法不行)

3)第二种方式jar cvfm jar包名称 主类清单文件 要打包的class文件

4) 用压缩软件进行压缩

3)以及4)方法生成的jar包,需要和class2json3_lib放在同一文件下才能运行

1. 准备工作:

为了省事,这里以eclipse中项目Class2Json为例,该项目结构如下:

d48f4d6137684892aa77f1072a9ab2b2.png

bin:是根据执行.java生成的.class文件,bin下的目录与src目录一样,只不过是.class文件

lib:外部依赖包

src:我们自己写的.java文件

004af0fdf453a596c0369239d67ae8c3.png

接下来:

1. 我们新建一个Class2Json_jar的文件夹2. 把bin下的文件复制到Class2Json_jar3. 并把lib整个复制过去,把lib改名为class2json3_lib4. 创建META-INF文件夹,并在该文件夹下创建MANIFEST.MF文件

在MANIFEST.MF文件中添加如下内容:(书写注意规范,中间都要用一个空格隔开,即使下一行,开头也要一个空格)

(第一个是版本号; 第二个是额外的jar包,第三个是启动的主类)

Manifest-Version: 1.0Class-Path: . class2json3_lib/commons-beanutils-1.8.0.jar class2json3_

lib/commons-collections-3.2.1.jar class2json3_lib/commons-lang-2.4.ja

r class2json3_lib/commons-logging-1.1.3.jar class2json3_lib/ezmorph-1.0.6.jar class2json3_lib/json-lib-2.3-jdk15.jar

Main-Class: com.zc.class2json.trans.test.TestDataClass2Json

我的Class2Json_jar文件夹包含如下内容:

6057a3a6881ed9b04f0afacbbf3b7241.png

2. jar命令打jar包

1) jar命令用法

zhangchao@zc:~/zc/jar-package-example/Class2Json_jar$ jar

Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...

Options:-c create newarchive-t list table of contents forarchive-x extract named (or all) files fromarchive-u update existing archive-v generate verbose output on standard output-f specify archive file name-m include manifest information fromspecified manifest file-n perform Pack200 normalization after creating a newarchive-e specify application entry point for stand-alone application

bundled into an executable jar file-0store only; use no ZIP compression-P preserve leading '/' (absolute path) and ".." (parent directory) components fromfile names-M do not create a manifest file forthe entries-i generate index information forthe specified jar files-C change to the specified directory and include the following file

If any fileis a directory then it isprocessed recursively.

The manifest file name, the archive file name and the entry point name are

specifiedin the same order as the 'm', 'f' and 'e'flags.

Example1: to archive two classfiles into an archive called classes.jar:

jar cvf classes.jar Foo.class Bar.classExample2: use an existing manifest file 'mymanifest'and archive all the

filesin the foo/ directory into 'classes.jar':

jar cvfm classes.jar mymanifest-C foo/ .

2)第一种方式 jar -cvf ./class2json.jar ./com  (这种方法不行)

jar -cvf ./class2json.jar ./com class2json.jar是jar包名称

./com 是将这个文件打入jar包

jar包内容如下所示:

b5f4cb7cecc3d3ef1ddca79ccde7c14c.png

其中META-INF/MANIFEST.MF是自动生成的,如下所示:并没有额外的jar包以及主类

Manifest-Version: 1.0Created-By: 1.8.0_181 (Oracle Corporation)

命令行如下所示:因为我们没指定运行的主类,所以 java -jar class2json.jar 运行失败,因为找不到主类

664c6d0a2b87d8b1c0f4aedb2c4bfc31.png

3)第二种方式jar cvfm jar包名称 主类清单文件 要打包的class文件

jar cvfm class2json_1.jar ./META-INF/MANIFEST.MF com

运行jar文件:

java -jar class2json_1.jar 参数1 参数2 参数3 参数4 参数5 参数6

这时把我们的主类清单添加到jar包里了

jar包内容如下所示:

bd0755ea34232b67d7da00020e3d77b6.png

其中META-INF/MANIFEST.MF是我们引入的,包括额外的jar包以及主类

Manifest-Version: 1.0

Class-Path: . class2json3_lib/commons-beanutils-1.8.0.jar class2json3_

lib/commons-collections-3.2.1.jar class2json3_lib/commons-lang-2.4.ja

r class2json3_lib/commons-logging-1.1.3.jar class2json3_lib/ezmorph-1

.0.6.jar class2json3_lib/json-lib-2.3-jdk15.jar

Created-By: 1.8.0_181 (Oracle Corporation)

Main-Class: com.zc.class2json.trans.test.TestDataClass2Json

命令行如下所示:指定运行的主类,所以 java -jar class2json.jar 运行成功

a04c0e6d859eba3bd393e9a2dc350dc7.png

4) 用压缩软件进行压缩

i)Ubuntu上,选中com以及META-INF文件夹,右键Compress:

4df0c79daec70055f96993c37b21518f.png

因为META-INF/MANIFEST.MF指明了jar包以及主类:

6cf4c14bc02436d778a0af36a662b0e9.png

ii)windows中,选中com以及META-INF文件夹,右键压缩,把后缀名改为jar

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值