如何在 Windows 7 上设置 java_home?
我进入控制面板“系统”中的环境变量并创建了两个新变量,一个用于用户变量,一个用于系统变量。两者都被命名为 JAVA_HOME 并且都指向
C:\Sun\SDK\jdk\bin
但由于某种原因,我在运行 Java 命令时仍然收到以下错误...
BUILD FAILED
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:161: The following error occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:141: The following error occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:137: Please set java.home to a JDK installation
Total time: 1 second
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish>lib\ant\bin\ant -f setup.xml
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib\tools.jar
Buildfile: setup.xml
我该如何解决这个问题?
I went to the Environment Variables in 'System' in the control panel and made two new variables, one for user variables and one for system variables. Both were named JAVA_HOME and both pointing to
C:\Sun\SDK\jdk\bin
But for some reason, I still get the below error when running a Java command...
BUILD FAILED
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:161: The following error occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:141: The following error occurred while executing this line:
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish\setup.xml:137: Please set java.home to a JDK installation
Total time: 1 second
C:\Users\Derek\Desktop\eclipse\eclipse\glassfish>lib\ant\bin\ant -f setup.xml
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\lib\tools.jar
Buildfile: setup.xml
How can I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
查找JDK安装目录
首先您需要知道Java开发工具包的安装路径。
打开JDK的默认安装路径:
应该有一个子目录,例如:
设置 JAVA_HOME 变量
获得 JDK 安装路径后:
完整的文章位于我的博客上:在 Windows 中设置 JAVA_HOME 变量。
Find JDK Installation Directory
First you need to know the installation path for the Java Development Kit.
Open the default installation path for the JDK:
There should be a subdirectory like:
Set the JAVA_HOME Variable
Once you have the JDK installation path:
The complete article is here, on my blog: Setting JAVA_HOME Variable in Windows.
对我有用的是将
%JAVA_HOME%\bin
添加到 Path 环境变量,其中JAVA_HOME
环境变量指向jdk
文件夹。What worked for me was adding the
%JAVA_HOME%\bin
to the Path environment variable with theJAVA_HOME
environment variable pointing to thejdk
folder.您必须首先安装JDK 在您的系统中。
设置 Java Home
JAVA_HOME = C:\Program Files\Java\jdk1.7.0 [JDK 安装目录的位置]
获得 JDK 安装路径后:
设置系统变量PATH下的JAVA路径
= C:\Program Files\Java\jdk1.7.0; [用分号附加值]
You have to first Install JDK in your system.
Set Java Home
JAVA_HOME = C:\Program Files\Java\jdk1.7.0 [Location of your JDK Installation Directory]
Once you have the JDK installation path:
Set JAVA Path under system variable
PATH= C:\Program Files\Java\jdk1.7.0; [Append Value with semi-colon]
在 cmd 中(暂时用于该 cmd 窗口):
In cmd (temporarily for that cmd window):
您需要将其设置为
C:\Sun\SDK\jdk
(假设这是安装 JDK 的位置 - 它不是默认值) - 不要将 \bin 放入C: \Sun\SDK\jdk\bin
。如果您的应用程序仅在您以当前用户身份登录时运行,则将其放入用户变量中 - 如果它需要为系统上的所有用户运行,则将其放入系统变量中。
您可能还需要将
%JAVA_HOME%\bin
添加到路径中(这还取决于您是仅从用户运行它还是从所有用户运行它,包括系统)You need to set it to
C:\Sun\SDK\jdk
(Assuming that is where the JDK is installed - It is not the default) - Do not put the \bin inC:\Sun\SDK\jdk\bin
.If your app only runs when you are logged in as the current user then put it in the user variables - If it needs to run for all users on your system then put it in System variables.
You might also need to add
%JAVA_HOME%\bin
to the path also (Also it depends on whether you run it from just the user or from all users, including System)http://javahowto.blogspot.com/2006/05/javahome-vs -javahome.html
控制面板> Java,Java 选项卡,单击查看按钮。在运行时参数中,输入:
或者,当您执行 Java 时,您可以将该命令行开关添加到命令中:
http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html
Control Panel > Java, Java tab, click the View button. In Runtime Parameters, put:
Or when you execute Java you can add that command line switch to the command:
一张图片就可以解决这个问题。
了解更多
One Image can fix this issue.
For More
对于那些仍然被这个问题困扰的人(我尝试了上述所有建议)——
如果您使用的是 64 位版本的 Windows 并且安装了 32 位 JDK,除了调整
PATH 变量,您可能还需要调整 registry 变量。
我抓狂了,正确设置了我的
PATH
变量 - 仍然无济于事 - 然后只在我的注册表中找到“空出的”Java 条目,这似乎是修复“失火”的死胡同Java 运行时环境。通过使用 Process Monitor 来观察我试图启动的程序,以便嗅出它在 Java(运行时环境)注册表中查找的位置,我成功地发现它正在查找 < Strong>32 位 版本的注册表项,位于
HKEY_LOCAL_MACHINE\SOFTWARE\**Wow6432Node**\JavaSoft\Java Runtime Environment
中。在该项中,您应该找到已安装的不同 Java 版本(过去和/或现在)的子项。单击最新版本的子项(例如,我的子项当前为1.7.0_25)。单击该子项后,您将看到右侧列出的注册表字符串值,特别是
JavaHome
和RuntimeLib
。您需要修改这两个值以分别反映当前文件夹和jvm.dll 文件。例如,在我的例子中,这些值(之前)分别设置在
C:\Program Files (x86)\Java\jre7
和C:\Program Files (x86)\Java\ jre7\bin\client\jvm.dll
在我的机器上不存在。我必须将它们更新到C:\Program Files (x86)\Java\jdk1.7.0_25\jre
和C:\Program Files (x86)\Java 的当前文件夹和文件\jdk1.7.0_25\jre\bin\client\jvm.dll
。同样,这完全取决于您安装的 Java 版本(JDK 和/或 JRE)(32 位或 64 位)以及您使用的操作系统类型(32 位或 64 位)。只需知道它们反映在注册表中的不同位置即可(例如 32 位应用程序的
Wow6432Node
,在我的例子中,32 位 JDK 安装在 64 位计算机上)。现在我已经更新了这两个注册表值,我的程序可以完美运行,不再出现问题或抱怨缺少 Java 运行时环境(源于注册表)。
For those who are still stumped with this problem (I tried all the above suggestions) --
If you're on a 64-bit version of Windows and you've installed the 32-bit JDK, besides adjusting
PATH
variables, you may need to adjust registry variables, too.I was pulling my hair out, having correctly set my
PATH
variables -- still to no avail -- and then only finding "vacated" Java entries in my registry, seemingly a deadend of fixing the "misfiring" Java Runtime Environment.By using
Process Monitor
to watch the program I was trying to get started, in order to sniff out where it was looking in the registry for Java (Runtime Environment), I triumphantly discovered that it's looking in the 32-bit version of registry entries, found inHKEY_LOCAL_MACHINE\SOFTWARE\**Wow6432Node**\JavaSoft\Java Runtime Environment
.Within that key, you should find subkeys of different Java versions installed (past and/or present). Click on the subkey of the latest version (my subkey is currently 1.7.0_25, for example). After clicking on that subkey, you'll see registry string values listed on the right, and particularly,
JavaHome
andRuntimeLib
. You need to modify the values of those two values to reflect the both the current folder and jvm.dll file, respectively.For example, in my case, the values were (previously) respectively set at
C:\Program Files (x86)\Java\jre7
andC:\Program Files (x86)\Java\jre7\bin\client\jvm.dll
which are nonexistent on my machine. I had to update these to the current folder and file ofC:\Program Files (x86)\Java\jdk1.7.0_25\jre
andC:\Program Files (x86)\Java\jdk1.7.0_25\jre\bin\client\jvm.dll
.Again, this will depend entirely on both what version of Java (JDK and/or JRE) you have installed -- 32 or 64-bit -- and what type of operating system you're on -- 32 or 64-bit. Just know that they're reflected in different locations within the registry (like the
Wow6432Node
for 32 bit applications, in my case with the 32-bit JDK installed on a 64-bit machine).Now that I've updated those two registry values, my program runs flawlessly, with no more hiccups or complaints about a missing Java Runtime Environment (stemming from the registry).
这是从 www.java 设置 Java 环境的官方解决方案 .com - 此处。
有适用于 Windows 7、Windows Vista、Windows XP、Linux/Solaris 和其他 shell 的解决方案。
例子
This is the official solution for setting the Java environment from www.java.com - here.
There are solutions for Windows 7, Windows Vista, Windows XP, Linux/Solaris and other shells.
Example
Windows 7
使用 Explorer(不是 Internet Explorer!)转至控制面板\所有控制面板项目\用户帐户
或
单击“开始”按钮
点击您的图片
更改我的环境变量
新...
(如果您没有足够的权限将其添加到“系统变量”部分,请将其添加到“用户变量”部分)
添加 JAVA_HOME 作为变量名称,将 JDK 位置添加为变量值 >好的
测试:
set JAVA_HOME
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
Windows 7
Go to Control Panel\All Control Panel Items\User Accounts using Explorer (not Internet Explorer!)
or
click on the Start button
click on your picture
Change my environment variables
New...
(if you don't have enough permissions to add it in the System variables section, add it to the User variables section)
Add JAVA_HOME as Variable name and the JDK location as Variable value > OK
Test:
set JAVA_HOME
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
我们需要区分这里互换讨论的两个环境变量。一是JAVA_HOME变量。另一个是Path变量。任何引用 JAVA_HOME 变量的进程都会寻找 JDK 的搜索路径,而不是 JRE。 JAVA_HOME 变量的使用并不意味着 Java 编译器本身。编译器知道它自己的位置。该变量旨在让其他软件更轻松地找到编译器。该变量通常由 IDE 软件使用,以便从 Java 源代码编译和构建应用程序。相比之下,Windows CMD 解释器和许多其他第一方和第三方软件引用 Path 变量,而不是 JAVA_HOME 变量。
用例 1:从 CMD 编译
例如,如果您没有使用任何 IDE 软件,而您只是希望能够从 CMD 进行编译,独立于当前的工作目录,那么您可以执行以下操作:想要的是正确设置 Path 变量。就您而言,您甚至不需要 JAVA_HOME 变量。因为 CMD 使用 Path 而不是 JAVA_HOME 来定位 Java 编译器。
用例 2:从 IDE 编译
但是,如果您正在使用某些 IDE 软件,那么您必须首先查看文档。它可能需要设置 JAVA_HOME,但也可能出于相同目的使用另一个变量名称。多年来事实上的标准一直是 JAVA_HOME,但情况可能并非总是如此。
用例 3:从 IDE 和 CMD 编译
如果除了 IDE 软件之外,您还希望能够从 CMD 进行编译,而与当前工作目录无关,那么另外 添加到 JAVA_HOME 变量中,您可能还需要将 JDK 搜索路径附加到 Path 变量中。
JAVA_HOME 与 Path
如果您的问题与编译 Java 相关,那么您需要检查 JAVA_HOME 变量和 Path(如果适用)。如果您的问题与运行 Java 应用程序有关,那么您需要检查 Path 变量。
路径变量在所有操作系统中通用。因为它是由系统定义的,并且是用于定位 JRE 的默认变量,所以运行 Java 应用程序几乎不会出现任何问题。尤其是在 Windows 上,软件安装程序通常会为您设置好一切。但是,如果您手动安装,最安全的做法可能是完全跳过 JAVA_HOME 变量,而仅使用 Path 变量来完成所有内容(对于 JDK 和 JRE)。任何最新版本的 IDE 软件都应该能够获取并使用它。
符号链接
符号链接可以提供另一种通过搭载现有环境变量来引用 JDK 搜索路径的方法。
我不确定以前版本的 Oracle/Sun JDK/JRE 版本,但至少 jdk1.8.0_74 的安装程序将搜索路径
C:\ProgramData\Oracle\Java\javapath
附加到路径变量,和将其放在字符串值的开头。该目录包含指向 JRE 目录中的 java.exe、javaw.exe 和 javaws.exe 的符号链接。因此,至少对于 Java 8 JDK,大概是 Java 8 JRE 独立版,不需要为 JRE 进行环境变量配置。只要使用安装包来设置就可以了。但是,您的 Windows 安装可能会有所不同。请注意,Oracle JRE 与 JDK 捆绑在一起。
如果您发现您的 Java JDK 配置使用了错误版本的编译器,或者它似乎神奇地工作,而没有明确定义(没有施咒),那么您的环境变量中可能有一个符号链接。因此您可能需要检查符号链接。
We need to make a distinction between the two environment variables that are discussed here interchangeably. One is the JAVA_HOME variable. The other is the Path variable. Any process that references the JAVA_HOME variable is looking for the search path to the JDK, not the JRE. The use of JAVA_HOME variable is not meant for the Java compiler itself. The compiler is aware of its own location. The variable is meant for other software to more easily locate the compiler. This variable is typically used by IDE software in order to compile and build applications from Java source code. By contrast, the Windows CMD interpreter, and many other first and third party software references the Path variable, not the JAVA_HOME variable.
Use case 1: Compiling from CMD
So for instance, if you are not using any IDE software, and you just want to be able to compile from the CMD, independent of your current working directory, then what you want is to set the Path variable correctly. In your case, you don't even need the JAVA_HOME variable. Because CMD is using Path, not JAVA_HOME to locate the Java compiler.
Use case 2: Compiling from IDE
However, if you are using some IDE software, then you have to look at the documentation first of all. It may require JAVA_HOME to be set, but it may also use another variable name for the same purpose. The de-facto standard over the years has been JAVA_HOME, but this may not always be the case.
Use case 3: Compiling from IDE and CMD
If in addition to the IDE software you also want to be able to compile from the CMD, independent of your current working directory, then in addition to the JAVA_HOME variable you may also need to append the JDK search path to the Path variable.
JAVA_HOME vs. Path
If your problem relates to compiling Java, then you want to check the JAVA_HOME variable, and Path (where applicable). If your problem relates to running Java applications, then you want to check your Path variable.
Path variable is used universally across all operating systems. Because it is defined by the system, and because it's the default variable that's used for locating the JRE, there is almost never any problem running Java applications. Especially not on Windows where the software installers usually set everything up for you. But if you are installing manually, the safest thing to do is perhaps to skip the JAVA_HOME variable altogether and just use the Path variable for everything, for both JDK and the JRE. Any recent version of an IDE software should be able to pick that up and use it.
Symlinks
Symbolic links may provide yet another way to reference the JDK search path by piggybacking one of the existing environment variables.
I am not sure about previous versions of Oracle/Sun JDK/JRE releases, but at least the installer for jdk1.8.0_74 appends the search path
C:\ProgramData\Oracle\Java\javapath
to the Path variable, and it puts it at the beginning of the string value. This directory contains symbolic links to the java.exe, javaw.exe and javaws.exe in the JRE directory.So at least with the Java 8 JDK, and presumably the Java 8 JRE standalone, no environment variable configuration needs to be done for the JRE. As long as you use the installer package to set it up. There may be differences on your Windows installation however. Note that the Oracle JRE comes bundled with the JDK.
If you ever find that your Java JDK configuration is using the wrong version of the compiler, or it appears to be working by magic, without being explicitly defined so (without casting the spell), then you may have a symlink somewhere in your environment variables. So you may want to check for symlink.
转到我的电脑(这台电脑)->右键->选择属性->高级系统设置->环境变量->在系统变量中点击“新建”按钮,在变量名中写入JAVA_HOME,路径为C:\Program Files\ Java\jdk1.8.0_131 变量值中存在 jdk->单击“确定”。
设置 JAVA_HOME 后关闭并重新打开命令提示符。有时更改不会反映在设置 JAVA_HOME 之前打开的 cmd 中。
您还可以通过终端本身设置 JAVA_HOME:
SET JAVA_HOME="C:\Program Files (x86)\Java\jdk1.8.0_131"
goto Mycomputer(This PC) -> rightclick ->select properties -> Advanced system settings -> environment variables-> in system variables click "New" button and write JAVA_HOME in variable name and path C:\Program Files\Java\jdk1.8.0_131 were jdk is present in variable value-> click ok.
close and reopen the command prompt after setting JAVA_HOME. Sometimes changes does not reflect in the cmd opened before setting the JAVA_HOME.
you can also set JAVA_HOME through terminal itself:
SET JAVA_HOME="C:\Program Files (x86)\Java\jdk1.8.0_131"
在 Eclipse 中:
Window->Preferences->Java->Installed JREs
使用搜索功能确保列出了最新的 Java 安装;然后确保它是被检查的那个。这应该是 JDK 而不是 JRE。
In Eclipse:
Window->Preferences->Java->Installed JREs
Use the search feature to make sure your latest Java installation is listed; then make sure it is the one that is checked. This should be a JDK not a JRE.
将 Java 目录添加到
PATH
变量时,您可能希望将其放在其开头。我遇到了一个问题,将 Java 目录放在PATH
的末尾是行不通的。经过检查,我在我的Windows\System32
目录中找到了java.exe
,当你的文件中有多个同名文件时,看起来第一个获胜。路径
...While adding your Java directory to your
PATH
variable, you might want to put it right at the beginning of it. I've had the problem, that putting the Java directory at the end of thePATH
would not work. After checking, I've foundjava.exe
in myWindows\System32
directory and it looks like the first one wins, when there are several files with the same name in yourPATH
...以管理员身份运行 Eclipse。
这解决了我的问题。我仍在挖掘其背后的逻辑。
Run Eclipse as Administrator.
That solved my problem. I'm still digging for the logic behind it.
如果安装jdk后没有重新启动计算机,只需重新启动计算机即可。
如果您想制作一个便携式 java 并在使用 java 之前设置路径,只需制作一个我在下面解释的批处理文件。
如果您想在计算机启动时运行此批处理文件,只需将批处理文件快捷方式放在启动文件夹中即可。在 Windows 7 启动文件夹中,“C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup”
创建一个批处理文件,如下所示:
注意:
java_home
和path
是变量。您可以根据需要创建任何变量。例如,
设置 amir=good_boy
,您可以通过%amir%
查看 amir,或者通过%java_home%
查看 java_homeif you have not restarted your computer after installing jdk just restart your computer.
if you want to make a portable java and set path before using java, just make a batch file i explained below.
if you want to run this batch file when your computer start just put your batch file shortcut in startup folder. In windows 7 startup folder is "C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
make a batch file like this:
note:
java_home
andpath
are variables. you can make any variable as you wish.for example
set amir=good_boy
and you can see amir by%amir%
or you can see java_home by%java_home%
经过几个小时的解决这里的大多数解决方案后,我只需安装 32 位 JDK 就解决了这个问题。
After hours of work around most of the solutions here, the problem was solved for me just by installing 32-bit JDK.
您可能想查看 Windows/System32 文件夹中的以下文件:
- java.exe
- javaw.exe
- javaws.exe
因为路径变量包含这些文件,所以你可能会很头疼地试图弄清楚为什么 java -version 或 javac -version 一直指向旧的 JAVA_HOME:重命名这些文件,去掉带下划线的扩展名, 例如。然后,您只需创建 JAVA_HOME 变量,将其添加到路径变量(例如%JAVA_HOME%)并附加“\bin”粒子。这样,您只需更改指向不同 JRE 或 JDK 的 JAVA_HOME 变量,并通过设置 JAVA_HOME 即可多次安装 java。
这也意味着最好手动安装 Java,无需安装程序,这样您就可以使用它。
You may wanna look into Windows/System32 folder for the following files:
- java.exe
- javaw.exe
- javaws.exe
Because, the path variable is including these files, you can get a headache trying to figure out why the java -version or javac -version keeps pointing to the old JAVA_HOME: rename those files, take out the extension with an underscore, for instance. And then, you only will have to create the JAVA_HOME variable, add it to the path variable (e.g., %JAVA_HOME%) and append the "\bin" particle. This way you need to only change the JAVA_HOME variable pointing to different JRE or JDK and have multiple installations of java just by setting JAVA_HOME.
This also means that it is better to install Java manually, without an installer, so you are able to toy with it.