设置 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;
设置 JDK环境变量(Windows)_第1张图片
设置 JDK环境变量(Windows)_第2张图片

【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)显然,在那里找不到类文件,然后再查看类路径:
    假定源文件包含指令:
import java.util.*;
import com.horstmann.corejava.*;
  • 并且源代码引用了 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目录树;

设置 JDK环境变量(Windows)_第3张图片

3.3)设置类路径 (调用的类库 不存在于当前目录或 CLASSPATH路径的时候,需要设置)

  • 3.3.1)最好采用 -classpath(或 -cp) 选项指定类路径:
java -classpath /home/user/classdir : . : /home/user/archives/archive.jar MyProg
或者
java -classpath c:\classdir; . ; c:\archives\archive.jar MyProg
  • 3.3.2)利用 -classpath 选项设置类路径是首选的方法, 也可以通过设置 CLASSPATH 环境变量完成这个操作;目录格式如下:
linux中: export CLASSPATH=/home/user/classdir: . : /home/user/archives/archives.jar
windows中: set CLASSPATH=c:\classdir; . ; c:\archives\archive.jar
  • 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.java

It 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.)

你可能感兴趣的:(java,JDK环境变量)