我們一般習慣用maven來管理、編譯項目,部署的時候很少在伺服器上再搭建一套maven環境。在部署項目時,需要將很多的依賴,多則上百個jar包加入到項目的庫中。
一般來說,我們會想到將jar包添加到classpath目錄中,過程如下:
1、轉到設定檔的目錄:cd etc
[root@db etc]# vi profile
2、如果已經配置過java環境,找到代碼塊:
JAVA_HOME=/usr/java/jdk1.8.0_45export HADOOP_PREFIX=/usr/local/hadoopPATH=$JAVA_HOME/bin:$PATH:$HADOOP_PREFIX/binCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport JAVA_HOMEexport PATHexport CLASSPATHexport 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\exttruesun.misc.Launcher$AppClassLoader@107077esun.misc.Launcher$AppClassLoader@107077ewhen 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:folderfalsesun.misc.Launcher$AppClassLoader@107077esun.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 MyJavaClassOr set the environment variable variable name lib Variable c: \ java \ axis-1_1 \ lib java-Djava.ext.dirs =% lib%-classpath classes MyJavaClass