Linux 下面設置JavaCLASSPATH變量

Linux 下面設置JavaCLASSPATH變量

設置了classpath 將會讓程序執行的更快html

export PATH="$JAVA_HOME/bin:"$PATH
export FILE_HOME=/root/test
echo $FILE_HOME
export CLASSPATH=.:$FILE_HOME/axis-1.4.jar:$JAVA_HOME/jre/lib/rt.jar:$FILE_HOME/axis-jaxrpc-1.4.jar:$FILE_HOME/axis-saaj-1.4.jar:$FILE_HOME/axis-wsdl4j-1.5.1.jar:$FILE_HOME/log4j-1.2.16.jar:$FILE_HOME/commons-logging-1.1.1.jar:$FILE_HOME/commons-discovery-0.2.jar
javac $FILE_HOME/TestTogate.java
java  -cp $CLASSPATH -Dcatalina.home="$FILE_HOME" TestTogate

除了把須要的jar都一一列舉以外,還能夠考慮使用通配符。java

例如以下結構:

Now, suppose the program uses a supporting library enclosed in a Jar file called supportLib.jar, physically in the directory D:\myprogram\lib\.windows

The corresponding physical file structure is :oracle

D:\myprogram\
      |
      ---> lib\
            |
            ---> supportLib.jar
      |
      ---> org\
            |
            --> mypackage\
                       |
                       ---> HelloWorld.class
                       ---> SupportClass.class
                       ---> UtilClass.class

We should use the following command-line option:app

java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld

or alternatively:less

set CLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar
java org.mypackage.HelloWorld

或者ide

java -classpath D:\myprogram;D:\myprogram\lib\* org.mypackage.HelloWorld

可能有不少人想不明白,我直接寫成以下不就能夠了嗎?ui

java -classpath D:\myprogram\*;D:\myprogram\lib\* org.mypackage.HelloWorld

但是事實證實這樣寫並不能夠。spa

爲何呢?官方文檔上面這樣說.net

Class Path Wild Cards

Class path entries can contain the base name wildcard character (*), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. For example, the class path entry mydir/* specifies all JAR files in the directory named mydir. A class path entry consisting of * expands to a list of all the jar files in the current directory. Files are considered regardless of whether they are hidden (have names beginning with '.').

A class path entry that contains an asterisk (*) does not match class files. To match both classes and JAR files in a single directory mydir, use either mydir:mydir/* or mydir/*:mydir. The order chosen determines whether the classes and resources in mydir are loaded before JAR files in mydir or vice versa.

Subdirectories are not searched recursively. For example, mydir/* searches for JAR files only in mydir, not in mydir/subdir1mydir/subdir2, and so on.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required, then the JAR files can be enumerated explicitly in the class path.

Expansion of wild cards is done early, before the invocation of a program's main method, rather than late, during the class-loading process. Each element of the input class path that contains a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory mydir contains a.jar, b.jar, and c.jar, then the class path mydir/* is expanded into mydir/a.jar:mydir/b.jar:mydir/c.jar, and that string would be the value of the system property java.class.path.

The CLASSPATH environment variable is not treated any differently from the -classpath or -cp options. Wild cards are honored in all of these cases. However, class path wild cards are not honored in the Class-Path jar-manifest header.

參考博文:

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

http://blog.csdn.net/bluishglc/article/details/9972951

相關文章
相關標籤/搜索