前尘回顾:
setup.bat
[chenquan@hostuser tartest]$ cat ../setup.sh
javac -encoding UTF-8 -Djava.ext.dirs=./ -d . ./SignNatureTest.java
echo compile finished,generate .class under package test at curdir
java -Djava.ext.dirs=./ test.SignNatureTest
1.生成jar包:sh hello.jar jar.sh
[chenquan@hostuser tartest]$ cat jar.sh jar -cvf0m ${1} ./META-INF/MANIFEST.MF .
[chenquan@hostuser tartest]$ ls META-INF/
MANIFEST.MF
[chenquan@hostuser tartest]$ ls test/
SignNatureTest.class
[chenquan@hostuser tartest]$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: test.SignNatureTest [chenquan@hostuser tartest]
2.运行jar 实现jar外部依赖:
准备将上一步生成jar包移动到 外部依赖的jar libs目录内或者 与lib同层级目录都可以,
java -jar -Djava.ext.dirs=./libs ${i}
如果在libs内 ,命令-Djava.ext.dirs=参数为./ ,如果生成jar与libs同级那么改为./libs 意思就是告诉jar 我的依赖jar包相对于我要执行的jar包的位置在哪里
[chenquan@hostuser tests]$ ll
total 12
-rw-rw-r--. 1 chenquan chenquan 3188 Aug 4 12:06 foo.jar
-rw-rw-r--. 1 chenquan chenquan 3254 Aug 4 12:23 hello.jar
drwxr-xr-x. 4 chenquan chenquan 135 Aug 4 12:14 libs
-rwxrwxrwx. 1 chenquan chenquan 104 Aug 4 12:36 start.sh
[chenquan@hostuser tests]$ ls libs/
commons-codec-1.12.jar commons-lang3-3.9.jar setup.sh SignNatureTest.java tartest test
[chenquan@hostuser tests]$ cat start.sh
#!/bin/bash
for i in "$@"
do
java -jar -Djava.ext.dirs=./libs ${i}
echo "excuting ${i} res is:"
done [chenquan@hostuser tests]$
执行结果,这里我比较懒写的sh文件不用每次敲一遍执行命令:
[chenquan@hostuser tests]$ sh start.sh hello.jar foo.jar
&k1=values1&k2=33
MD5string is: 832c599beef71cce2ca7e3953b4dc5fb2019-08-04 12:50:20excuting hello.jar res is:
&k1=values1&k2=33
MD5string is: 832c599beef71cce2ca7e3953b4dc5fb2019-08-04 12:50:21excuting foo.jar res is:
[chenquan@hostuser tests]$
补充:Classpath
- Class-Path设置为
.
是为了声明从哪个目录开始查找所有的引用类的jar包所在位置