Java開(kāi)發(fā)中常用到環(huán)境變量的配置菱属,下面簡(jiǎn)單介紹下Java中經(jīng)常配置的環(huán)境變量:PATH和CLASSPATH描焰。
1单山、PATH環(huán)境變量
1.1 作用簡(jiǎn)介
安裝完JDK(Java Development Kit逸爵,Java開(kāi)發(fā)套件)之后,可以在安裝目錄下找到兩個(gè)子目錄(bin目錄和lib目錄)士嚎。bin目錄中包含著Java編譯器等可執(zhí)行文件呜魄。
如果要運(yùn)行執(zhí)行java命令,必須得執(zhí)行java命令對(duì)應(yīng)的可執(zhí)行文件的路徑莱衩,通常有兩種方式:
- 在%JAVA_HOME%目錄下執(zhí)行爵嗅。
- 執(zhí)行命令的時(shí)候,指明路徑%JAVA_HOME%/bin/java
但是笨蚁,這樣不是特別方便睹晒,這就是為什么配置環(huán)境變量。如果將%JAVA_HOME%/bin/
括细,添加到環(huán)境變量PATH中伪很。再執(zhí)行java命令時(shí)(無(wú)論在哪個(gè)目錄下執(zhí)行),系統(tǒng)就會(huì)從左到右搜索(這里的順序很重要奋单,可以利用這個(gè)特性覆蓋掉某個(gè)舊版本的jdk锉试。)環(huán)境變量PATH中執(zhí)行的目錄,直到找到對(duì)應(yīng)的可執(zhí)行文件并執(zhí)行(找到之后览濒,后面的目錄都會(huì)被忽略掉)呆盖。如果找不到拖云,提示該命令不存在。這就是PATH環(huán)境變量的作用应又。
1.2 如何配置
另外宙项,由于JDK的安裝目錄中的%JAVA_HOME%/jre/bin
目錄下也有一些常用的工具,所以一般也將其配置到PATH環(huán)境變量中株扛。同時(shí)尤筐,在配置java環(huán)境的同事,不能影響其它環(huán)境的運(yùn)行席里。所以,以windows下面環(huán)境變量的配置(各個(gè)目錄之間用;
隔開(kāi))為例拢驾,通常將下面的內(nèi)容加到PATH環(huán)境變量的最左側(cè):
%JAVA_HOME%/bin/;%JAVA_HOME%/jre/bin
2奖磁、CLASSPATH環(huán)境變量
2.1 作用簡(jiǎn)介
和PATH變量不同,CLASSPATH環(huán)境變量的作用是指定Java類(lèi)所在的目錄(或許它的意思就是PATH of Class
)繁疤。
當(dāng)運(yùn)行java程序的時(shí)候咖为,要指定相應(yīng)的類(lèi)名,比如稠腊,下面的例子中躁染,在C:\test\
目錄下寫(xiě)一個(gè)HelloWorld,并執(zhí)行:
c:\test>type HelloWorld.java #查看文本文件的內(nèi)容
public class HelloWorld{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
c:\test>javac HelloWorld.java #因?yàn)榕渲昧薖ATH環(huán)境變量架忌,在任意目錄下都可執(zhí)行javac
c:\test>dir #查看編譯生成的class文件
2016/03/28 22:13 427 HelloWorld.class
2016/03/28 22:08 152 HelloWorld.java
c:\test>java HelloWorld #運(yùn)行HelloWorld(注意吞彤,不能加.class后綴)
錯(cuò)誤: 找不到或無(wú)法加載主類(lèi) HelloWorld
這里報(bào)錯(cuò)找不到或無(wú)法加載主類(lèi) HelloWorld
,前面說(shuō)到
CLASSPATH環(huán)境變量的作用是指定Java類(lèi)所在的目錄叹放。
下面看下此時(shí)環(huán)境中CLASSPATH環(huán)境變量的值是什么:
c:\test>echo %CLASSPATH%
C:\Program Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program Files\Java\jdk1.8.0_51\lib\dt.jar
c:\test>
果真饰恕,沒(méi)有HelloWorld.class所在的目錄。下面我們通過(guò)手動(dòng)指定CLASSPATH解決該問(wèn)題:
c:\test>java -classpath . HelloWorld
Hello World!!
c:\test>
實(shí)際上井仰,和PATH環(huán)境變量也是由左到右搜索的埋嵌,所以,在向CLASSPATH中添加新的目錄時(shí)俱恶,通常將其放在最左側(cè)雹嗦。下面的例子中,在指定-classpath
選項(xiàng)的參數(shù)時(shí)合是,引用了%CLASSPATH%環(huán)境變量:
c:\test>java -classpath .;%CLASSPATH% HelloWorld
錯(cuò)誤: 找不到或無(wú)法加載主類(lèi) Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program
c:\test>java -classpath ".;%CLASSPATH%" HelloWorld
Hello World!!
c:\test>echo ".;%CLASSPATH%"
".;C:\Program Files\Java\jdk1.8.0_51\lib\tools.jar;C:\Program Files\Java\jdk1.8.0_51\lib\dt.jar"
ps:如果剛裝完JDK了罪,沒(méi)有配置環(huán)境變量,那么缺省的%CLASSPATH%環(huán)境變量的值是.
聪全,也就是當(dāng)前目錄捶惜。
2.2 通常如何配置
Java中通常將環(huán)境變量CLASSPATH配置為.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar
。其中為何包含.
荔烧,在上面的例子中已經(jīng)看得很清楚了吱七。下面的內(nèi)容大概介紹了另外兩個(gè)的作用:
dt.jar:運(yùn)行環(huán)境類(lèi)庫(kù)汽久,主要是Swing包,這一點(diǎn)通過(guò)用壓縮軟件打開(kāi)dt.jar也可以看到踊餐。如果在開(kāi)發(fā)時(shí)候沒(méi)有用到Swing包景醇,那么可以不用將dt.jar添加到CLASSPATH變量中。
tools.jar:工具類(lèi)庫(kù)吝岭,它跟我們程序中用到的基礎(chǔ)類(lèi)庫(kù)沒(méi)有關(guān)系三痰。我們注意到在Path中變量值bin目錄下的各個(gè)exe工具的大小都很小,一般都在27KB左右窜管,這是因?yàn)樗鼈儗?shí)際上僅僅相當(dāng)于是一層代碼的包裝散劫,這些工具的實(shí)現(xiàn)所要用到的類(lèi)庫(kù)都在tools.jar中,用壓縮軟件打開(kāi)tools.jar,你會(huì)發(fā)現(xiàn)有很多文件是和bin目錄下的exe工具相對(duì)性的幕帆,如下圖:
tools_jarFrom:The use of CLASSPATH
2.3 指定CLASSPATH的注意事項(xiàng)
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.Folders and Archive Files
When classes are stored in a directory (folder), such as c:\java\MyClasses\utility\myapp, then the class path entry points to the directory that contains the first element of the package name (in this case, C:\java\MyClasses, because the package name is utility.myapp).
When classes are stored in an archive file (a zip or JAR file) the class path entry is the path to and including the zip or JAR file. For example, the command to use a class library that is in a JAR file as follows:java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Multiple Specifications
To find class files in the directory C:\java\MyClasses and classes in C:\java\OtherClasses, you would set the class path to the following. Note that the two paths are separated by a semicolon.
java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Specification Order
The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the previous example, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only when it does not find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.
From: docs.oracle.com
3获搏、JAVA_HOME環(huán)境變量
這個(gè)是不重要的,題目中我都沒(méi)有提它失乾。它唯一的作用就是常熙,前面兩個(gè)環(huán)境變量的配置中引用了它,所以碱茁,要將其配置為:
C:\Program Files\Java\jdk1.8.0_51\
如果前面環(huán)境變量的配置都顯式指定了完成的路徑裸卫,那么完全可以不用配置JAVA_HOME環(huán)境變量。