項目部署:多個jar包加入到classpath

來源:互聯網
上載者:User

我們一般習慣用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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.