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

http://blog.csdn.net/wzygis/article/details/48750929

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

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

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

[html] view plain copy
  1. [root@db etc]# vi profile  

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

[html] view plain copy
  1. JAVA_HOME=/usr/java/jdk1.8.0_45  
  2. export HADOOP_PREFIX=/usr/local/hadoop  
  3. PATH=$JAVA_HOME/bin:$PATH:$HADOOP_PREFIX/bin  
  4. CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  
  5. export JAVA_HOME  
  6. export PATH  
  7. export CLASSPATH  
  8. export LD_LIBRARY_PATH=$HADOOP_HOME/lib/native/  

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

[html] view plain copy
  1. 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

[html] view plain copy
  1. 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).  
  2.   
  3. Assuming that in our library, we have a class named Lib. And our application runs this code:  
  4.   
  5. public class Main {  
  6.     public static void main(String args[]) {  
  7.         System.out.println(System.getProperty("java.ext.dirs"));  
  8.         ClassLoader test_cl = Main.class.getClassLoader();  
  9.         ClassLoader lib_cl = Lib.class.getClassLoader();  
  10.         System.out.println(test_cl == lib_cl);  
  11.         System.out.println(test_cl);  
  12.         System.out.println(lib_cl);  
  13.     }  
  14. }  
  15. The console output will be:  
  16.   
  17. C:\Program Files\Java\jdk1.6.0\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext  
  18. true  
  19. sun.misc.Launcher$AppClassLoader@107077e  
  20. sun.misc.Launcher$AppClassLoader@107077e  
  21. when the application is run using the command java -cp "folder/*;." Main.  
  22.   
  23. However, when the application is run using the command java -Djava.ext.dirs=folder Main, then the output will instead be:  
  24.   
  25. folder  
  26. false  
  27. sun.misc.Launcher$AppClassLoader@107077e  
  28. 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

[html] view plain copy
  1. java  -Djava.ext.dirs=/usr/local/dependency  xx.Start  

xx.Start 为项目的main方法所在类。

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

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

[html] view plain copy
  1. java  -Djava.ext.dirs=%lib%  xx.Start  

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

[html] view plain copy
  1. <plugin>    
  2.                     <groupId>org.apache.maven.plugins</groupId>    
  3.                     <artifactId>maven-jar-plugin</artifactId>    
  4.                     <version>2.4</version>    
  5.                     <configuration>    
  6.                         <archive>    
  7.                             <manifest>    
  8.                                 <addClasspath>true</addClasspath>    
  9.                                 <classpathPrefix>lib/</classpathPrefix>    
  10.                                 <mainClass>com.mobile263.cloudsimcdr.console.Start</mainClass>    
  11.                             </manifest>    
  12.                         </archive>    
  13.                     </configuration>    
  14.                 </plugin>  



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

[html] view plain copy
  1. 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.  
  2. There is a neat trick - use the "java.ext.dirs" JVM param to point to the directory containing the jar files.  
  3. java-Djava.ext.dirs = c: \ java \ axis-1_1 \ lib-classpath classes MyJavaClass  
  4. Or set the environment variable variable name lib   
  5. Variable c: \ java \ axis-1_1 \ lib   
  6. java-Djava.ext.dirs =% lib%-classpath classes MyJavaClass  

猜你喜欢

转载自hunan.iteye.com/blog/2360333