目录:
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为例,该项目结构如下:
bin:是根据执行.java生成的.class文件,bin下的目录与src目录一样,只不过是.class文件
lib:外部依赖包
src:我们自己写的.java文件
接下来:
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文件夹包含如下内容:
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包内容如下所示:
其中META-INF/MANIFEST.MF是自动生成的,如下所示:并没有额外的jar包以及主类
Manifest-Version: 1.0Created-By: 1.8.0_181 (Oracle Corporation)
命令行如下所示:因为我们没指定运行的主类,所以 java -jar class2json.jar 运行失败,因为找不到主类
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包内容如下所示:
其中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 运行成功
4) 用压缩软件进行压缩
i)Ubuntu上,选中com以及META-INF文件夹,右键Compress:
因为META-INF/MANIFEST.MF指明了jar包以及主类:
ii)windows中,选中com以及META-INF文件夹,右键压缩,把后缀名改为jar