简体   繁体   English

如何在 Windows 中为 Java 设置环境变量

[英]How to set the environment variables for Java in Windows

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

Java SE Development Kit 8u112 on a 64-bit Windows 7 or Windows 8 64 位 Windows 7 或 Windows 8 上的 Java SE Development Kit 8u112

Set the following user environment variables (== environment variables of type user variables )设置以下用户环境变量(== user variables类型的环境变量)

  • JAVA_HOME : C:\Program Files\Java\jdk1.8.0_112 JAVA_HOME : C:\Program Files\Java\jdk1.8.0_112
  • JDK_HOME : %JAVA_HOME% JDK_HOME : %JAVA_HOME%
  • JRE_HOME : %JAVA_HOME%\jre JRE_HOME : %JAVA_HOME%\jre
  • CLASSPATH : .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib CLASSPATH : .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
  • PATH : your-unique-entries;%JAVA_HOME%\bin (make sure that the longish your-unique-entries does not contain any other references to another Java installation folder. PATH : your-unique-entries;%JAVA_HOME%\bin (确保较长your-unique-entries不包含对另一个 Java 安装文件夹的任何其他引用。

Note for Windows users on 64-bit systems: 64 位系统上的 Windows 用户注意事项:

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

Notice that these environment variables are derived from the "root" environment variable JAVA_HOME .请注意,这些环境变量是从“根”环境变量JAVA_HOME派生的。 This makes it easy to update your environment variables when updating the JDK.这使得在更新 JDK 时更新环境变量变得容易。 Just point JAVA_HOME to the fresh installation.只需将JAVA_HOME指向全新安装即可。

There is a blogpost explaining the rationale behind all these environment variables . 有一篇博文解释了所有这些环境变量背后的基本原理

Optional recommendations可选建议

  • Add an user environment variable JAVA_TOOL_OPTIONS with value -Dfile.encoding="UTF-8" .添加值为-Dfile.encoding="UTF-8"用户环境变量JAVA_TOOL_OPTIONS This ensures that Java (and tools such as Maven ) will run with a Charset.defaultCharset() of UTF-8 (instead of the default Windows-1252 ).这确保了 Java(和Maven等工具)将使用UTF-8Charset.defaultCharset() (而不是默认的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 .在使用我自己的代码和其他人的代码时,这节省了很多麻烦,不幸的是,这些代码经常假设(理智的)默认编码UTF-8
  • When JDK is installed, it adds to the system environment variable Path an entry C:\ProgramData\Oracle\Java\javapath;安装 JDK 时,它会在系统环境变量Path中添加一个条目C:\ProgramData\Oracle\Java\javapath; . . I anecdotally noticed that the links in that directory didn't get updated during an JDK installation update.有趣的是,我注意到该目录中的链接在 JDK 安装更新期间没有得到更新。 So it's best to remove C:\ProgramData\Oracle\Java\javapath;所以最好去掉C:\ProgramData\Oracle\Java\javapath; from the Path system environment variable in order to have a consistent environment.Path系统环境变量中获得一致的环境。

In Windows inorder to set在 Windows 中为了设置

Step 1 : Right Click on MyComputer and click on properties .第 1 步:右键单击 MyComputer 并单击属性。

Step 2 : Click on Advanced tab第 2 步:单击高级选项卡

替代文字

Step 3: Click on Environment Variables第三步:点击环境变量

替代文字

Step 4: Create a new class path for JAVA_HOME第 4 步:为 JAVA_HOME 创建一个新的类路径

替代文字

Step 5: Enter the Variable name as JAVA_HOME and the value to your jdk bin path ie c:\Programfiles\Java\jdk-1.6\bin and第 5 步:输入变量名称为 JAVA_HOME 和值到您的 jdk bin 路径,即 c:\Programfiles\Java\jdk-1.6\bin 和

NOTE Make sure u start with .;注意确保你以.;开头。 in the Value so that it doesn't corrupt the other environment variables which is already set.在值中,这样它就不会破坏已经设置的其他环境变量。

替代文字

Step 6 : Follow the Above step and edit the Path in System Variables add the following ;c:\Programfiles\Java\jdk-1.6\bin in the value column.第 6 步:按照上述步骤并编辑系统变量中的路径,在值列中添加以下;c:\Programfiles\Java\jdk-1.6\bin

Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type第 7 步:您已完成为 Java 设置环境变量,为了对其进行测试,请转到命令提示符并键入

 java   

who will get a list of help doc谁将获得帮助文档列表

In order make sure whether compiler is setup Type in cmd为了确保编译器是否设置在cmd中键入

  javac

who will get a list related to javac谁将获得与 javac 相关的列表

Hope this Helps !希望这可以帮助 !

--- To set java path --- --- 设置 java 路径 ---

There are two ways to set java path有两种设置java路径的方法

A. Temporary A. 临时

  1. Open cmd打开命令
  2. Write in cmd : javac在cmd中写入: javac

If java is not installed, then you will see message:如果未安装 java,您将看到以下消息:

javac is not recognized as internal or external command, operable program or batch file . javac 不是内部或外部命令、可运行程序或批处理文件

  1. Write in cmd : set path=C:\Program Files\Java\jdk1.8.0_121\bin在cmd中写入: set path=C:\Program Files\Java\jdk1.8.0_121\bin
  2. Write in cmd : javac在cmd中写入: javac

You can check that path is set if not error has been raised.如果没有引发错误,您可以检查是否设置了路径。

It is important to note that these changes are only temporary from programs launched from this cmd.需要注意的是,这些更改只是从此 cmd 启动的程序中的临时更改。

NOTE: You might have to run the command line as admin注意:您可能必须以管理员身份运行命令行

B. Permanent B. 永久

  1. Righ-click on "My computer" and click on properties右键单击“我的电脑”,然后单击属性
  2. Click on "Advanced system settings"点击“高级系统设置”
  3. Click on "Environment variables"点击“环境变量”
  4. Click on new tab of user variable单击用户变量的新选项卡
  5. Write path in variable namevariable name中写入path
  6. Copy the path of bin folder复制bin文件夹的路径
  7. Paste the path of the bin folder in the variable valuebin文件夹的路径粘贴到variable value
  8. Click OK点击确定

The path is now set permanently.路径现在已永久设置。

TIP: The tool "Rapid Environment Editor" (freeware) is great for modifying the environment variables and useful in that case提示:“快速环境编辑器”工具(免费软件)非常适合修改环境变量,在这种情况下很有用

TIP2: There is also a faster way to access the Environment Variables: press Win + R keys, paste the following %windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables and press ENTER TIP2:还有一种访问环境变量的更快方法:按Win + R键,粘贴以下%windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables并按ENTER

In Windows 7, right-click on Computer -> Properties -> Advanced system settings ;在 Windows 7 中,右键单击计算机->属性->高级系统设置 then in the Advanced tab, click Environment Variables... -> System variables -> New... .然后在Advanced选项卡中,单击Environment Variables... -> System variables -> New...

Give the new system variable the name JAVA_HOME and the value C:\Program Files\Java\jdk1.7.0_79 (depending on your JDK installation path it varies).将新系统变量命名为JAVA_HOME和值C:\Program Files\Java\jdk1.7.0_79 (取决于您的 JDK 安装路径,它会有所不同)。

Then select the Path system variable and click Edit... .然后选择Path系统变量并单击Edit... Keep the variable name as Path , and append C:\Program Files\Java\jdk1.7.0_79\bin;保持变量名称为Path ,并附加C:\Program Files\Java\jdk1.7.0_79\bin; or %JAVA_HOME%\bin;%JAVA_HOME%\bin; (both mean the same) to the variable value. (两者含义相同)到变量值。

Once you are done with above changes, try below steps.完成上述更改后,请尝试以下步骤。 If you don't see similar results, restart the computer and try again.如果您没有看到类似的结果,请重新启动计算机并重试。 If it still doesn't work you may need to reinstall JDK.如果它仍然不起作用,您可能需要重新安装 JDK。

Open a Windows command prompt (Windows key + R -> enter cmd -> OK ), and check the following:打开 Windows 命令提示符(Windows 键 + R -> 输入cmd -> OK ),然后检查以下内容:

java -version

You will see something like this:你会看到这样的东西:

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Then check the following:然后检查以下内容:

javac -version

You will see something like this:你会看到这样的东西:

javac 1.7.0_79

The JDK installation instructions explain exactly how to set the PATH , for different versions of Windows. JDK 安装说明准确解释了如何为不同版本的 Windows 设置PATH

Normally you should not set the CLASSPATH environment variable.通常不应设置CLASSPATH环境变量。 If you leave it unset, Java will look in the current directory to find classes.如果您不设置它,Java 将在当前目录中查找类。 You can use the -cp or -classpath command line switch with java or javac .您可以将-cp-classpath命令行开关与javajavac一起使用。

I am going to explain here by pictures for Windows 7.我将在这里通过 Windows 7 的图片进行解释。

Please follow the following steps:请按照以下步骤操作:

Step 1: Go to "Start" and get into the "My Computer" properties第 1 步:转到“开始”并进入“我的电脑”属性

在此处输入图像描述

Step 2: Go to "Advance System Setting" and click on it.第2步:进入“高级系统设置”并点击它。

在此处输入图像描述

Step 3: Go to "Start" and get into the "My Computer" properties第 3 步:转到“开始”并进入“我的电脑”属性

在此处输入图像描述

Step 4: The dialog for Environment variable will open like this:第 4 步:环境变量对话框将如下打开:

在此处输入图像描述

Step 5: Go to path and click on edit.第5步:转到路径并单击编辑。

在此处输入图像描述

Step 6: Put the path of your JDK wherever it resides up to bin like you can see in the picture.第 6 步:将 JDK 的路径放在 bin 中,如图所示。 Also add path from your sdk of Android up to the Platform Tools:还要添加从您的 Android sdk 到平台工具的路径:

在此处输入图像描述

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

And of course, you can retrieve them from Java using:当然,您可以使用以下方法从 Java 中检索它们:

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

In programming context you can execute SET command (SET classpath=c:\java) or Right click on your computer > properties > advanced > environment variables.在编程上下文中,您可以执行 SET 命令 (SET classpath=c:\java) 或右键单击您的计算机 > 属性 > 高级 > 环境变量。

In a batch file you can use在批处理文件中,您可以使用

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

For Windows 7 users:对于 Windows 7 用户:

Right-click on My Computer, select Properties;右击我的电脑,选择属性; Advanced;先进的; System Settings;系统设置; Advanced;先进的; Environment Variables.环境变量。 Then find PATH in the second box and set the variable like in the picture below.然后在第二个框中找到 PATH 并设置变量,如下图所示。

PATH 变量编辑器

Keep in mind that the %CLASSPATH% environment variable is ignored when you use java/javac in combination with one of the -cp , -classpath or -jar arguments.请记住,当您将 java/javac 与-cp-classpath-jar参数之一结合使用时,将忽略%CLASSPATH%环境变量。 It is also ignored in an IDE like Netbeans/Eclipse/IntelliJ/etc.它在像 Netbeans/Eclipse/IntelliJ/etc 这样的 IDE 中也会被忽略 It is only been used when you use java/javac without any of the above mentioned arguments.在您使用没有上述任何参数的 java/javac 时使用。

In case of JAR files, the classpath is to be defined as class-path entry in the manifest.mf file.对于 JAR 文件,类路径将被定义为 manifest.mf 文件中的class-path条目。 It can be defined semicolon separated and relative to the JAR file's root.它可以定义为以分号分隔并相对于 JAR 文件的根目录。

In case of an IDE, you have the so-called 'build path' which is basically the classpath which is used at both compiletime and runtime.在 IDE 的情况下,您拥有所谓的“构建路径”,它基本上是在编译时和运行时都使用的类路径。 To add external libraries you usually drop the JAR file in a (either precreated by IDE or custom created) lib folder of the project which is added to the project's build path.要添加外部库,您通常会将 JAR 文件放在项目的(由 IDE 预先创建或自定义创建的) lib文件夹中,该文件夹将添加到项目的构建路径中。

Set java Environment variable in Centos / Linux在 Centos / Linux 中设置 java 环境变量

/home/ vi .bashrc /home/ vi .bashrc

export JAVA_HOME=/opt/oracle/product/java/jdk1.8.0_45导出 JAVA_HOME=/opt/oracle/product/java/jdk1.8.0_45

export PATH=$JAVA_HOME/bin:$PATH导出 PATH=$JAVA_HOME/bin:$PATH

java -version java版本

For deployment better to set up classpath exactly and keep environment clear.为了更好地部署,准确设置类路径并保持环境清晰。 Or at *.bat (the same for linux, but with correct variables symbols):或在 *.bat (对于 linux 相同,但具有正确的变量符号):

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

Or at command line or *.bat (for *.sh too) if classpath id not very long:如果类路径 id 不是很长,或者在命令行或 *.bat(对于 *.sh 也是):

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

For Windows:对于 Windows:

  • Right click on 'My Computers' and open 'Properties'.右键单击“我的电脑”并打开“属性”。
  • In Windows Vista or Windows 7, go to "Advanced System Settings".在 Windows Vista 或 Windows 7 中,转到“高级系统设置”。 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.选择“系统变量”列表下的“路径”,然后按编辑并在分号后添加C:\Program Files\java\jdk\bin
  • 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_HOME”作为变量名和 jdk 主目录的路径(例如,如果您正在安装 java 版本 6,则为“C:\Program Files\Java\jdk1.6.0_24”。目录name 可能会随着 diff.java 版本的不同而改变)作为 variable_value。
  1. Download the JDK下载 JDK
  2. Install it安装它
  3. Then Setup environment variables like this :然后像这样设置环境变量:
  4. Click on EDIT点击编辑

在此处输入图像描述

  1. Then click PATH , Click Add , Then Add it like this:然后单击PATH ,单击 Add ,然后像这样添加它: 在此处输入图像描述

Java path set for java 11java 11设置的 Java 路径

  1. copy the path for jdk-11复制 jdk-11 的路径

Don't include the \bin folder, just the JDK path.不要包含 \bin 文件夹,只包含 JDK 路径。 For example例如

CorrectC:\Program Files\Java\jdk-11正确- C:\Program Files\Java\jdk-11

WrongC:\Program Files\Java\jdk-11\bin错误- C:\Program Files\Java\jdk-11\bin

In environmental variable , user variable section click on New button and give path like below.环境变量中, user variable部分单击新建按钮并给出如下所示的路径。 在此处输入图像描述

after that give ok for it and go to the System variables and select the Path and double click on it.之后给它确定并转到System variables并选择Path并双击它。

在此处输入图像描述

click on new and paste %JAVA_HOME%\bin and click ok to all.单击新建并粘贴%JAVA_HOME%\bin并单击全部确定。 在此处输入图像描述

您的 Keytools 文件位于“Java/bin”文件夹下,因此您需要设置环境变量或转到“Java/bin”文件夹并运行命令

You can add JAVA_HOME in the system environment variable from my computer>>advance tab>add the new path as explained here .您可以从我的计算机>>高级选项卡>添加新路径在系统环境变量中添加JAVA_HOME ,如此所述。

It might help Mac and Linux users as well.它也可能对 Mac 和 Linux 用户有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM