如何设置Java环境变量在Windows(类路径)?


当前回答

Windows:

Right click on 'My Computers' and open 'Properties'. In Windows Vista or Windows 7, go to "Advanced System Settings". Else go to next step. Go to 'Advanced Tab' and click on Environment Variables button. Select 'Path' under the list of 'System Variables', and press Edit and add C:\Program Files\java\jdk\bin after a semicolon. Now click on 'new' button under system variables and enter 'JAVA_HOME' as variable name and path to jdk home directory (ex. 'C:\Program Files\Java\jdk1.6.0_24' if you are installing java version 6. Directory name may change with diff. java versions) as variable_value.

其他回答

Java 11的Java路径设置

复制jdk-11的路径

不要包含\bin文件夹,只包含JDK路径。例如

正确答案是C:\Program Files\Java\jdk-11

错,是C:\Program Files\Java\jdk-11\bin

在环境变量中,用户变量部分单击新建按钮并给出如下路径。

然后给它一个ok,然后进入系统变量,选择路径,双击它。

点击新建,粘贴%JAVA_HOME%\bin,然后点击确定全部。

Windows:

Right click on 'My Computers' and open 'Properties'. In Windows Vista or Windows 7, go to "Advanced System Settings". Else go to next step. Go to 'Advanced Tab' and click on Environment Variables button. Select 'Path' under the list of 'System Variables', and press Edit and add C:\Program Files\java\jdk\bin after a semicolon. Now click on 'new' button under system variables and enter 'JAVA_HOME' as variable name and path to jdk home directory (ex. 'C:\Program Files\Java\jdk1.6.0_24' if you are installing java version 6. Directory name may change with diff. java versions) as variable_value.

JDK安装说明详细解释了如何针对不同版本的Windows设置PATH。

通常不应该设置CLASSPATH环境变量。如果不设置,Java将在当前目录中查找类。您可以在java或javac中使用-cp或-classpath命令行开关。

为了部署,最好准确地设置类路径并保持环境清晰。 或者at *.bat (linux也是一样的,但是变量符号是正确的):

CLASSPATH="c:\lib;d:\temp\test.jar;<long classpath>"
CLASSPATH=%CLASSPATH%;"<another_logical_droup_of_classpath" 
java -cp %CLASSPATH% com.test.MainCLass

或者在命令行或*.bat(对于*.sh也是),如果类路径id不是很长:

java -cp "c:\lib;d:\temp\test.jar;<short classpath>"

Linux下:http://lowfatlinux.com/linux-environment-variables.html

当然,你可以使用以下方法从Java中检索它们:

String variable = System.getProperty("mykey");