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


当前回答

Java SE Development Kit 8u112, 64位Windows 7或Windows 8

设置以下用户环境变量(==类型为用户变量的环境变量)

JAVA_HOME: C:\Program Files\Java\ jdk1.8.0_112 Jdk_home: % java_home % JRE_HOME: %JAVA_HOME%\jre 类路径:.;%JAVA_HOME%\lib 路径:your-unique-entries;%JAVA_HOME%\bin(确保较长的your-unique-entries不包含对其他Java安装文件夹的任何其他引用。

64位系统的Windows用户注意:

Progra~1 = 'Program Files'
Progra~2 = 'Program Files(x86)'

注意,这些环境变量派生自“根”环境变量JAVA_HOME。这使得在更新JDK时可以很容易地更新环境变量。只需将JAVA_HOME指向新的安装。

有一篇博文解释了所有这些环境变量背后的基本原理。

可选的建议

Add a user environment variable JAVA_TOOL_OPTIONS with value -Dfile.encoding="UTF-8". This ensures that Java (and tools such as Maven) will run with a Charset.defaultCharset() of UTF-8 (instead of the default Windows-1252). This has saved a lot of headaches when wirking with my own code and that of others, which unfortunately often assume the (sane) default encoding UTF-8. When JDK is installed, it adds to the system environment variable Path an entry C:\ProgramData\Oracle\Java\javapath;. I anecdotally noticed that the links in that directory didn't get updated during an JDK installation update. So it's best to remove C:\ProgramData\Oracle\Java\javapath; from the Path system environment variable in order to have a consistent environment.

其他回答

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 7用户:

右键单击“我的电脑”,选择“属性”;先进的;系统设置;先进的;环境变量。然后在第二个框中找到PATH并设置如下图所示的变量。

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.

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

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

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

在编程环境中,您可以执行SET命令(SET classpath=c:\java)或右键单击您的计算机>属性>高级>环境变量。

在批处理文件中可以使用

SET classpath=c:\java
java c:\myapplication.class