您的位置:首页 > 运维架构 > Linux

Linux 下面设置JavaCLASSPATH变量

2016-04-20 00:00 323 查看
摘要: Linux 下面设置JavaCLASSPATH变量

Linux 下面设置JavaCLASSPATH变量

设置了classpath 将会让程序执行的更快

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都一一列举之外,还可以考虑使用通配符。

例如如下结构:

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

The corresponding physical file structure is :

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

We should use the following command-line option:

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

or alternatively:

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

或者

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

可能有很多人想不明白,我直接写成如下不就可以了吗?

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

可是事实证明这样写并不可以。

为什么呢?官方文档上面这样说

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/subdir1
,
mydir/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息