项目部署:多个jar包加入到classpath

我们一般习惯用maven来管理、编译项目,部署的时候很少在服务器上再搭建一套maven环境。在部署项目时,需要将很多的依赖,多则上百个jar包加入到项目的库中。

一般来说,我们会想到将jar包添加到classpath目录中,过程如下:

1、转到配置文件的目录:cd etc

[root@db etc]# vi profile


2、如果已经配置过java环境,找到代码块:

JAVA_HOME=/usr/java/jdk1.8.0_45
export HADOOP_PREFIX=/usr/local/hadoop
PATH=$JAVA_HOME/bin:$PATH:$HADOOP_PREFIX/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
export LD_LIBRARY_PATH=$HADOOP_HOME/lib/native/


3、在CLASSPATH后面逐个添加jar包。

CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:xxxx1.jar:xxxx2.jar

分析一下,这样的配置方法相当于在程序中将参数写死,一旦jar包变更,得重新配置,很不灵活。


最近在stack overflow上发现一篇文章:

CLASSPATH vs java.ext.dirs  http://stackoverflow.com/questions/5039862/classpath-vs-java-ext-dirs

One difference is that if we specify our libraries under the -Djava.ext.dirs flag, they will be loaded using the extension classloader (e.g. sun.misc.Launcher.ExtClassLoader) instead of the system classloader (e.g. sun.misc.Launcher.AppClassLoader).

Assuming that in our library, we have a class named Lib. And our application runs this code:

public class Main {
    public static void main(String args[]) {
        System.out.println(System.getProperty("java.ext.dirs"));
        ClassLoader test_cl = Main.class.getClassLoader();
        ClassLoader lib_cl = Lib.class.getClassLoader();
        System.out.println(test_cl == lib_cl);
        System.out.println(test_cl);
        System.out.println(lib_cl);
    }
}
The console output will be:

C:\Program Files\Java\jdk1.6.0\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
true
sun.misc.Launcher$AppClassLoader@107077e
sun.misc.Launcher$AppClassLoader@107077e
when the application is run using the command java -cp "folder/*;." Main.

However, when the application is run using the command java -Djava.ext.dirs=folder Main, then the output will instead be:

folder
false
sun.misc.Launcher$AppClassLoader@107077e
sun.misc.Launcher$ExtClassLoader@7ced01


总结解决方法:java给我们提供了一个扩展jar包的参数配置参数:java.ext.dirs

1、利用maven导出全部的依赖jar包,请参考上一篇文章:

http://blog.csdn.net/wzygis/article/details/48735133  maven导出项目依赖的jar包

2、将依赖的jar包上传到的服务器后,放到一个目录下,比如:dependencies

java  -Djava.ext.dirs=/usr/local/dependency  xx.Start
xx.Start 为项目的main方法所在类。


3、另外一种方法是配置环境变量。

设置环境变量  变量名lib 变量值c:\java\axis-1_1\lib

java  -Djava.ext.dirs=%lib%  xx.Start

4、可以在maven中设置启动Main方法启动类。

<plugin>  
				    <groupId>org.apache.maven.plugins</groupId>  
				    <artifactId>maven-jar-plugin</artifactId>  
				    <version>2.4</version>  
				    <configuration>  
				        <archive>  
				            <manifest>  
				                <addClasspath>true</addClasspath>  
				                <classpathPrefix>lib/</classpathPrefix>  
				                <mainClass>com.mobile263.cloudsimcdr.console.Start</mainClass>  
				            </manifest>  
				        </archive>  
				    </configuration>  
				</plugin>


配上参考文章,方便理解:

If U are in a hurry to compile code and there is a big list of Jar files to include in the classpath, there U don't have to struggle to include the names of all the jar's in the classpath.
There is a neat trick - use the "java.ext.dirs" JVM param to point to the directory containing the jar files.
java-Djava.ext.dirs = c: \ java \ axis-1_1 \ lib-classpath classes MyJavaClass
Or set the environment variable variable name lib 
Variable c: \ java \ axis-1_1 \ lib 
java-Djava.ext.dirs =% lib%-classpath classes MyJavaClass


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值