http://blog.csdn.net/wzygis/article/details/48750929
我们一般习惯用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