java classpath linux_Linux 下面设置JavaCLASSPATH变量

本文详细介绍了在Linux系统中如何设置Java CLASSPATH变量,包括通过export命令添加路径和jar文件,以及使用通配符简化classpath设定。讨论了通配符在类路径中的使用规则,强调了正确设置的重要性,并提醒读者注意通配符不匹配类文件以及子目录不会递归搜索的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

参考博文:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值