Java環(huán)境變量PATH和CLASSPATH

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_jar

From: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)境變量。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末纽竣,一起剝皮案震驚了整個(gè)濱河市墓贿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蜓氨,老刑警劉巖募壕,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異语盈,居然都是意外死亡舱馅,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)刀荒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)代嗤,“玉大人,你說(shuō)我怎么就攤上這事缠借「梢悖” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵泼返,是天一觀的道長(zhǎng)硝逢。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么渠鸽? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任叫乌,我火速辦了婚禮,結(jié)果婚禮上徽缚,老公的妹妹穿的比我還像新娘憨奸。我一直安慰自己,他們只是感情好凿试,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布排宰。 她就那樣靜靜地躺著,像睡著了一般那婉。 火紅的嫁衣襯著肌膚如雪板甘。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天详炬,我揣著相機(jī)與錄音盐类,去河邊找鬼。 笑死痕寓,一個(gè)胖子當(dāng)著我的面吹牛傲醉,可吹牛的內(nèi)容都是我干的蝇闭。 我是一名探鬼主播呻率,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼呻引!你這毒婦竟也來(lái)了礼仗?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤逻悠,失蹤者是張志新(化名)和其女友劉穎元践,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體童谒,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡单旁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了饥伊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片象浑。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖琅豆,靈堂內(nèi)的尸體忽然破棺而出愉豺,到底是詐尸還是另有隱情,我是刑警寧澤茫因,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布蚪拦,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏驰贷。R本人自食惡果不足惜盛嘿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饱苟。 院中可真熱鬧孩擂,春花似錦、人聲如沸箱熬。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)城须。三九已至蚤认,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間糕伐,已是汗流浹背砰琢。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留良瞧,地道東北人陪汽。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像褥蚯,于是被迫代替她去往敵國(guó)和親挚冤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容