设置 JDK环境变量(Windows)
【0】README
0.1) 本文轉自 core java volume 1,旨在說明如何設置 JDK環境變量,以及為什么要設置的問題;
【1】JDK目錄樹
Attention)就Java 而言, docs 和 src 是兩個最有用的子目錄:因為 docs 目錄 包含了HTML 格式的類庫文件;而 src目錄 包含了 Java 類庫中公共部分的源代碼;
【2】設置執行路徑(路徑不要包含空格)
2.1)執行路徑:它是指os 搜索本地可執行文件的目錄列表;(在安裝完JDK后,我們還要把 jdk/bin 目錄添加到 執行路徑中), jdk里面全是可執行文件,如 編譯器javac.exe 和解釋器 java.exe , 抽取注釋工具 javadoc.exe 等;
2.2)如何測試 jdk 設置是否正確: java -version;
【3】設置類庫路徑(CLASSPATH環境變量)
Attention)
- A1)由于運行時庫文件(rt.jar 和在 jre/lib 與 jre/lib/ext 目錄下的一些其他JAR文件) 會被自動搜索,所以不必將它們顯式地列在類路徑中;
- A2) 如何搜索包中的文件路徑, 參見 http://blog.csdn.net/pacosonswjtu/article/details/49536995 中的“小節【4】( 將類放入包) “的內容;
Alert) javac 編譯器總是在當前等級目錄中查找文件, 但 java虛擬機僅在類路徑中有 “.” 目錄的時候才查看當前目錄;
- A1)如果沒有設置類路徑, 那也并不會產生什么問題,默認的類路徑包含“.”目錄(當前目錄);
- A2)如果設置類路徑忘記了包含“.”目錄,則程序仍 然可以通過編譯, 但卻無法運行(故,設置類路徑,定要設置當前文件夾 ‘.’);
3.1)看個荔枝, 虛擬機如何搜尋類文件:
- step1)它首先要查看存儲在 jre/lib 和 jre/lib/ext 目錄下的歸檔文件中所存放的系統類文件;
- step2)顯然,在那里找不到類文件,然后再查看類路徑:
假定源文件包含指令:
- 并且源代碼引用了 Employee類;那么 編譯器將試圖查找 java.lang.Employee(因為java.lang 包被默認導入)、java.util.Employee、com.horstmann.corejava.Employee 和 當前包中的Employee。對這個類路徑的所有位置中所列出的每一個類進行逐一查看;如果找到了一個以上的類,就會發生編譯錯誤,因為類必須是唯 一的;
- 編譯器的任務不止這些: 它還要查看源文件是否比類文件新。如果是這樣的話,那么源文件就會被自動地重新編譯;
3.2)設置CLASSPATH 環境變量(為什么設置 CLASSPATH為 .;C:\jdk1.8.0_60\lib ?) 參見JDK目錄樹;
3.3)設置類路徑 (調用的類庫 不存在于當前目錄或 CLASSPATH路徑的時候,需要設置)
- 3.3.1)最好采用 -classpath(或 -cp) 選項指定類路徑:
- 3.3.2)利用 -classpath 選項設置類路徑是首選的方法, 也可以通過設置 CLASSPATH 環境變量完成這個操作;目錄格式如下:
- Attention)直到 退出shell 為止, 類路徑設置均有效;
【4】Oracle official tutorial ——how to update your PATH Environment Variable
following contents are reshipped from http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html
Updating the PATH Environment Variable (Optional)
You can run the JDK without setting the PATH environment variable, or you can optionally set it so that you can conveniently run the JDK executable files (javac.exe, java.exe, javadoc.exe, and so forth) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as:
C:\> "C:\Program Files\Java\jdk1.7.0\bin\javac" MyClass.javaIt is useful to set the PATH variable permanently so it will persist after rebooting.
To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the PATH variable. Typically, this full path looks something like C:\Program Files\Java\jdk1.7.0\bin. Set the PATH variable as follows on Microsoft Windows:
- Click Start, then Control Panel, then System.
- Click Advanced, then Environment Variables.
- Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:
- C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.7.0\bin
Note:
- The PATH environment variable is a series of directories separated by semicolons (;) and is not case-sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
- You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored.
- If you are not sure where to add the path, add it to the right of the value of the PATH variable.
- The new path takes effect in each new command window you open after setting the PATH variable.
Starting to Use the JDK
If you are new to developing and running programs in the Java programming language, see The Java Tutorial online for some guidance. Note especially the tutorial trails under the heading Trails Covering the Basics.
You can also download the JDK documentation from the Java SE Downloads page.
Uninstalling the JDK
If you should ever want to uninstall the JDK, use the “Add/Remove Programs” utility in the Microsoft Windows Control Panel.
Installed Directory Tree
See JDK and JRE File Structure for a description of the directory structure of the JDK. (Note that the file structure of the JRE is identical to that of the JDK’s jre directory.)
總結
以上是生活随笔為你收集整理的设置 JDK环境变量(Windows)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 相关术语
- 下一篇: 紫藤萝瀑布写作背景是什么 紫藤萝瀑布介绍