1. 程式人生 > >專案部署:多個jar包加入到classpath

專案部署:多個jar包加入到classpath

我們一般習慣用maven來管理、編譯專案,部署的時候很少在伺服器上再搭建一套maven環境。在部署專案時,需要將很多的依賴,多則上百個jar包加入到專案的庫中。

一般來說,我們會想到將jar包新增到classpath目錄中,過程如下:

1、轉到配置檔案的目錄:cd etc

[[email protected] 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上發現一篇文章:

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
[email protected]
[email protected] 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 [email protected] [email protected]


總結解決方法:java給我們提供了一個擴充套件jar包的引數配置引數:java.ext.dirs

1、利用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