[英]How to set the environment variables for Java in Windows
如何在 Windows(类路径)中为 Java 设置环境变量?
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 . 有一篇博文解释了所有这些环境变量背后的基本原理。
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
).UTF-8
的Charset.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 .Path
an entry C:\ProgramData\Oracle\Java\javapath;
Path
中添加一个条目C:\ProgramData\Oracle\Java\javapath;
. 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. 临时
javac
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 不是内部或外部命令、可运行程序或批处理文件。
set path=C:\Program Files\Java\jdk1.8.0_121\bin
set path=C:\Program Files\Java\jdk1.8.0_121\bin
javac
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. 永久
path
in variable name
variable name
中写入path
variable value
variable value
中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
命令行开关与java
或javac
一起使用。
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 并设置变量,如下图所示。
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:
C:\Program Files\java\jdk\bin
after a semicolon.C:\Program Files\java\jdk\bin
。Java path set for java 11为java 11设置的 Java 路径
Don't include the \bin folder, just the JDK path.不要包含 \bin 文件夹,只包含 JDK 路径。 For example
例如
Correct – C:\Program Files\Java\jdk-11
正确-
C:\Program Files\Java\jdk-11
Wrong – C:\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”文件夹并运行命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.