java安装的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1672281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Environment variables for java installation
提问by Dean jones
How to set the environment variables for Java in Windows (the classpath)?
如何在 Windows(类路径)中为 Java 设置环境变量?
回答by Lastnico
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");
回答by Cem Kalyoncu
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
回答by Jesper
The JDK installation instructionsexplain exactly how to set the PATH
, for different versions of Windows.
在JDK安装说明解释究竟如何设置PATH
,为不同版本的Windows。
Normally you should not set the CLASSPATH
environment variable. If you leave it unset, Java will look in the current directory to find classes. You can use the -cp
or -classpath
command line switch with java
or javac
.
通常你不应该设置CLASSPATH
环境变量。如果不设置它,Java 将在当前目录中查找类。您可以将-cp
or-classpath
命令行开关与java
or 一起使用javac
。
回答by St.Shadow
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:
或者在命令行或 *.bat (对于 *.sh 也是)如果类路径 ID 不是很长:
java -cp "c:\lib;d:\temp\test.jar;<short classpath>"
回答by Srinivas M.V.
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
第 3 步:单击环境变量
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 和
NOTEMake 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 !
希望这可以帮助 !
回答by BalusC
Keep in mind that the %CLASSPATH%
environment variable is ignoredwhen you use java/javac in combination with one of the -cp
, -classpath
or -jar
arguments. It is also ignoredin an IDE like Netbeans/Eclipse/IntelliJ/etc. It is onlybeen used when you use java/javac without any of the above mentioned arguments.
请记住,当您将 java/javac 与,或参数之一结合使用时,%CLASSPATH%
环境变量将被忽略。它在像 Netbeans/Eclipse/IntelliJ/etc 这样的 IDE 中也被忽略。只有在没有上述任何参数的情况下使用 java/javac时才使用它。-cp
-classpath
-jar
In case of JAR files, the classpath is to be defined as class-path
entry in the manifest.mf file. It can be defined semicolon separated and relative to the JAR file's root.
对于 JAR 文件,类路径将定义为class-path
manifest.mf 文件中的条目。它可以用分号分隔并相对于 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. 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.
在 IDE 的情况下,您有所谓的“构建路径”,它基本上是在编译时和运行时使用的类路径。要添加外部库,您通常将 JAR 文件放在项目的(由 IDE 预先创建的或自定义创建的)lib
文件夹中,该文件夹被添加到项目的构建路径中。
回答by Pratap Singh
For 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 Variablesbutton.
- Select 'Path' under the list of 'System Variables', and press Editand 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.
- 右键单击“我的电脑”并打开“属性”。
- 在 Windows Vista 或 Windows 7 中,转到“高级系统设置”。否则进入下一步。
- 转到“高级选项卡”并单击Environment Variables按钮。
- 在“系统变量”列表下选择“路径”,然后按Edit并
C:\Program Files\java\jdk\bin
在分号后添加。 - 现在单击系统变量下的“新建”按钮并输入“JAVA_HOME”作为变量名和 jdk 主目录的路径(例如,“C:\Program Files\Java\jdk1.6.0_24”,如果您安装的是 Java 版本 6。目录名称可能会随着不同的 Java 版本而改变)作为 variable_value。
回答by Jawad Zeb
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 并设置如下图所示的变量。
回答by Abdull
Java SE Development Kit 8u112 on a 64-bit Windows 7 or Windows 8
64 位 Windows 7 或 Windows 8 上的 Java SE 开发工具包 8u112
Set the following user environment variables(== environment variables of type user variables)
设置以下用户环境变量(==用户变量类型的环境变量)
JAVA_HOME :
C:\Program Files\Java\jdk1.8.0_112
JDK_HOME :
%JAVA_HOME%
JRE_HOME :
%JAVA_HOME%\jre
CLASSPATH :
.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
PATH :
your-unique-entries;%JAVA_HOME%\bin
(make sure that the longishyour-unique-entries
does not contain any other references to another Java installation folder.
JAVA_HOME :
C:\Program Files\Java\jdk1.8.0_112
JDK_HOME :
%JAVA_HOME%
JRE_HOME :
%JAVA_HOME%\jre
CLASSPATH :
.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
PATH :
your-unique-entries;%JAVA_HOME%\bin
(确保 longishyour-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
. This makes it easy to update your environment variables when updating the JDK. Just point JAVA_HOME
to the fresh installation.
请注意,这些环境变量源自“root”环境变量JAVA_HOME
。这使得在更新 JDK 时更新环境变量变得容易。只需指向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"
. This ensures that Java (and tools such as Maven) will run with aCharset.defaultCharset()
ofUTF-8
(instead of the defaultWindows-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 entryC:\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 removeC:\ProgramData\Oracle\Java\javapath;
from thePath
system environment variable in order to have a consistent environment.
- 添加值为的用户环境变量。这可确保 Java(以及诸如Maven 之类的工具)将使用of (而不是默认的)运行。这在处理我自己和其他人的代码时省去了很多麻烦,不幸的是,这些代码通常假设(理智)默认编码UTF-8。
JAVA_TOOL_OPTIONS
-Dfile.encoding="UTF-8"
Charset.defaultCharset()
UTF-8
Windows-1252
- 安装 JDK 后,它会在系统环境变量中添加
Path
一个条目C:\ProgramData\Oracle\Java\javapath;
. 我偶然注意到该目录中的链接在 JDK 安装更新期间没有更新。所以最好C:\ProgramData\Oracle\Java\javapath;
从Path
系统环境变量中移除,以获得一致的环境。
回答by hitesh141
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. Also add path from your sdk of Android up to the Platform Tools:
第 6 步:将 JDK 的路径放到 bin 中,就像您在图片中看到的那样。还要添加从您的 Android sdk 到平台工具的路径: