实践jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ jar --help
Illegal option: -
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-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
    -n  perform Pack200 normalization after creating a new archive
    -e  specify application entry point for stand-alone application 
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -P  preserve leading '/' (absolute path) and ".." (parent directory) components from file names
    -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, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ jar --help
非法选项: -
用法: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
选项包括: 
    -c  创建新的归档文件
    -t  列出归档目录
    -x  从档案中提取指定的 (或所有) 文件
    -u  更新现有的归档文件
    -v  在标准输出中生成详细输出
    -f  指定归档文件名【jar-file】
    -m  包含指定清单文件中的清单信息【manifest-file】
    -e  为捆绑到可执行 jar 文件的独立应用程序【entry-point】【主类】
        指定应用程序入口点
    -0  仅存储; 不使用情况任何 ZIP 压缩
    -M  不创建条目的清单文件
    -i  为指定的 jar 文件生成索引信息
    -C  更改为指定的目录并包含其中的文件【指定files的目录】
如果有任何目录文件, 则对其进行递归处理。
清单文件名, 归档文件名和入口点名称的指定顺序
与 'm', 'f''e' 标记的指定顺序相同。

示例 1: 将两个类文件归档到一个名为 classes.jar 的归档文件中: 
       jar cvf classes.jar Foo.class Bar.class 
示例 2: 使用现有的清单文件 'mymanifest' 并
           将 foo/ 目录中的所有文件归档到 'classes.jar' 中: 
       jar cvfm classes.jar mymanifest -C foo/ .

用法

法一

1、构建monitor.jar

所有的依赖包全部放置在lib目录下,monitor.jar中META-INF\MANIFEST.MF内容如下:

1
2
3
4
5
6
7
8
9
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0-b64 (Sun Microsystems Inc.)
Main-Class: com.allinpay.monitor.server.Monitor
Class-Path: lib/activemq-all-5.3.0.jar lib/commons-logging.jar lib/ger
onimo-j2ee-management_1.0_spec-1.0.jar lib/geronimo-jms_1.1_spec-1.1.
1.jar lib/log4j-1.2.14.jar lib/mina-core-1.1.7.jar lib/slf4j-api-1.5.
6.jar lib/slf4j-simple-1.5.6.jar lib/spring.jar lib/xbean-spring-3.6.
jar

2、运行

1
$ java -jar Monitor.jar

法二

1、构建jms-activemq-demo-1.0.0-SNAPSHOT.jar

所有依赖的第三方包放置在/app/target目录下,META-INF\MANIFEST.MF内容参考如下:

1
2
3
4
5
6
7
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Angi
Build-Jdk: 1.6.0_38
Main-Class: com.allinpay.its.jms.activemq.demo.queue.requestreply.consumer.JmsListenerSample
Class-Path: .

2、运行

1
$ java -Djava.ext.dirs=/app/target -jar jms-activemq-demo-1.0.0-SNAPSHOT.jar

法三

不需要处理MANIFEST.MF,手动指定Main类

1
$ java -Djava.ext.dirs=./lib com.allinpay.its.watch.notify.main.Main

法四

使用-cp或-classpath,classpath:即用户类路径,就是一些包含类文件的目录, jar和zip文件的列表,可以使用通配符(*) ,多个路径用“:”分隔(linux)或“;”(windows)。

例如:

1
$ java -cp .:./*:lib/* wang.ray.demo.Main

.:表示在当前目录下搜索类文件

./*:表示在当前目录下所有jar或zip文件中搜索类文件

lib/*:表示当前目录下的lib目录下的所有jar或zip文件中搜索类文件

推荐使用classpath,不推荐使用java.ext.dirs

工具

Fat Jar Eclipse Plug-In

在Java Project上右键,选择Build Fat Jar,默认会将本项目和所有依赖的jar包全部打到一个jar包中