如何在 Windows 7 上设置 java_home?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2619584/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:58:30  来源:igfitidea点击:

How to set java_home on Windows 7?

javawindows-7java-homesystem-variable

提问by Derek

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

我转到控制面板中“系统”中的环境变量并创建了两个新变量,一个用于用户变量,一个用于系统变量。两者都被命名为 JAVA_HOME 并且都指向

C:\Sun\SDK\jdk\bin

C:\Sun\SDK\jdk\bin

But for some reason, I still get the below error when running a Java command...

但是出于某种原因,我在运行 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

How can I fix this problem?

我该如何解决这个问题?

回答by Ricket

http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html

http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html

Control Panel > Java, Java tab, click the View button. In Runtime Parameters, put:

控制面板 > Java,Java 选项卡,单击查看按钮。在运行时参数中,输入:

-Djava.home=YOUR_PATH_HERE

Or when you execute Java you can add that command line switch to the command:

或者,当您执行 Java 时,您可以将该命令行开关添加到命令中:

java -Djava.home=PATH SomeJavaApp

回答by Romain Hippeau

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 in C:\Sun\SDK\jdk\bin.

您需要将其设置为C:\Sun\SDK\jdk(假设这是安装 JDK 的位置 - 它不是默认设置) - 不要将 \bin 放入C:\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%\binto the path also (Also it depends on whether you run it from just the user or from all users, including System)

您可能还需要添加%JAVA_HOME%\bin到路径中(这也取决于您是仅从用户还是从所有用户(包括系统)运行它)

回答by fogedi

What worked for me was adding the %JAVA_HOME%\binto the Path environment variable with the JAVA_HOMEenvironment variable pointing to the jdkfolder.

对我有用的是将%JAVA_HOME%\bin环境变量添加到 Path 环境变量中,JAVA_HOME环境变量指向jdk文件夹。

回答by muscailie

Run Eclipseas Administrator.

以管理员身份运行Eclipse

That solved my problem. I'm still digging for the logic behind it.

那解决了我的问题。我仍在挖掘其背后的逻辑。

回答by Berg

In Eclipse: Window->Preferences->Java->Installed JREs

在日食中: 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 安装;然后确保它是被检查的那个。这应该是 JDK 而不是 JRE。

回答by Robs

Find JDK Installation Directory

查找JDK安装目录

First you need to know the installation path for the Java Development Kit.

首先需要知道Java Development Kit的安装路径。

Open the default installation path for the JDK:

打开JDK的默认安装路径:

C:\Program Files\Java

There should be a subdirectory like:

应该有一个子目录,如:

C:\Program Files\Java\jdk1.8.0_172

Note:one has only to put the path to the jdk without /bin in the end (as suggested on a lot of places). e.g. C:\Java\jdk1.8.0_172 and NOT C:\Java\jdk1.8.0_172\bin !

注意:最后只需要在没有 /bin 的情况下放置 jdk 的路径(正如很多地方所建议的那样)。例如 C:\Java\jdk1.8.0_172 而不是 C:\Java\jdk1.8.0_172\bin !



Set the JAVA_HOME Variable

设置 JAVA_HOME 变量

Once you have the JDK installation path:

获得 JDK 安装路径后:

  1. Right-click the My Computericon on your desktop and select Properties.
  2. Click the Advancedtab, then click the Environment Variablesbutton.
  3. Under System Variables, click New.
  4. Enter the variable name as JAVA_HOME.
  5. Enter the variable value as the installation path for the Java Development Kit.
  6. Click OK.
  7. Click Apply Changes.
  1. 右键单击桌面上的“我的电脑”图标,然后选择“属性”
  2. 单击高级选项卡,然后单击环境变量按钮。
  3. 系统变量下,单击新建。
  4. 输入变量名作为JAVA_HOME
  5. 输入变量值作为 Java Development Kit 的安装路径。
  6. 单击“确定”
  7. 单击应用更改

Note:You might need to restart Windows

注意:您可能需要重新启动 Windows

The complete article is here, on my blog: Setting JAVA_HOME Variable in Windows.

完整的文章在这里,在我的博客上:在 Windows 中设置 JAVA_HOME 变量

回答by HATCHA

This is the official solutionfor setting the Java environment from www.java.com - here.

这是从 www.java.com- here设置 Java 环境的官方解决方案

There are solutions for Windows 7, Windows Vista, Windows XP, Linux/Solaris and other shells.

有针对 Windows 7、Windows Vista、Windows XP、Linux/Solaris 和其他 shell 的解决方案。



Example

例子

Windows 7

  1. Select Computer from the Start menu
  2. Choose System Properties from the context menu
  3. Click Advanced system settings -> Advanced tab
  4. Click on Environment Variables, under System Variables, find PATH, and click on it.
  5. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
  6. Reopen Command prompt window, and run your Java code.

Windows 7的

  1. 从开始菜单中选择计算机
  2. 从上下文菜单中选择系统属性
  3. 单击高级系统设置 -> 高级选项卡
  4. 单击环境变量,在系统变量下,找到 PATH,然后单击它。
  5. 在编辑窗口中,通过将类的位置添加到 PATH 的值来修改 PATH。如果您没有 PATH 项,您可以选择添加一个新变量并添加 PATH 作为名称和类的位置作为值。
  6. 重新打开命令提示符窗口,然后运行您的 Java 代码。

回答by user2414128

While adding your Java directory to your PATHvariable, 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 the PATHwould not work. After checking, I've found java.exein my Windows\System32directory and it looks like the first one wins, when there are several files with the same name in your PATH...

在将 Java 目录添加到PATH变量时,您可能希望将它放在变量的开头。我遇到了问题,将 Java 目录放在最后是PATH行不通的。检查后,我java.exe在我的Windows\System32目录中找到了,当您的目录中有多个同名文件时,看起来第一个获胜PATH

回答by Ankit jain

You have to first Install JDKin your system.

您必须首先在您的系统中安装 JDK

Set Java Home

设置 Java 主页

JAVA_HOME= C:\Program Files\Java\jdk1.7.0[Location of your JDK Installation Directory]

JAVA_HOME= C:\Program Files\Java\jdk1.7.0[JDK 安装目录的位置]

Once you have the JDK installation path:

获得 JDK 安装路径后:

  • Right-click the My Computer icon on
  • Select Properties.
  • Click the Advanced system setting tab on left side of your screen
  • Aadvance Popup is open.
  • Click on Environment Variables button.
  • 右键单击我的电脑图标
  • 选择属性。
  • 单击屏幕左侧的高级系统设置选项卡
  • 高级弹出窗口已打开。
  • 单击环境变量按钮。

enter image description here

在此处输入图片说明

  • Under System Variables, click New.
  • Enter the variable name as JAVA_HOME.
  • Enter the variable value as the installation path for the Java Development Kit.
  • Click OK.
  • Click Apply Changes.
  • 在系统变量下,单击新建。
  • 输入变量名作为 JAVA_HOME。
  • 输入变量值作为 Java Development Kit 的安装路径。
  • 单击确定。
  • 单击应用更改。

Set JAVA Path under system variable

在系统变量下设置JAVA Path

PATH= C:\Program Files\Java\jdk1.7.0; [Append Value with semi-colon]

PATH= C:\Program Files\Java\jdk1.7.0; [用分号附加值]

check here

在这里检查

回答by Coldblackice

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 PATHvariables, you may need to adjust registryvariables, too.

如果您使用的是 64 位版本的 Windows 并且您已经安装了 32 位 JDK,那么除了调整PATH变量之外,您可能还需要调整注册表变量。

I was pulling my hair out, having correctly set my PATHvariables -- still to no avail -- and then only finding "vacated" Java entries in my registry, seemingly a deadend of fixing the "misfiring" Java Runtime Environment.

我正在拔毛,正确设置了我的PATH变量 - 仍然无济于事 - 然后只在我的注册表中找到“空出”的 Java 条目,这似乎是修复“失火”Java 运行时环境的死胡同。

By using Process Monitorto 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-bitversion of registry entries, found in HKEY_LOCAL_MACHINE\SOFTWARE\**Wow6432Node**\JavaSoft\Java Runtime Environment.

通过使用Process Monitor观看我试图开始的程序,为了找出它在 Java(运行时环境)的注册表中查找的位置,我成功地发现它正在查找32 位版本的注册表项,在HKEY_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, JavaHomeand RuntimeLib. You need to modify the values of those two values to reflect the both the currentfolder and jvm.dll file, respectively.

在该键中,您应该找到安装的不同 Java 版本(过去和/或现在)的子键。单击最新版本的子项(例如我的子项当前为 1.7.0_25)。单击该子项后,您将看到右侧列出的注册表字符串值,特别是JavaHomeRuntimeLib。您需要修改这两个值的值以分别反映当前文件夹和 jvm.dll 文件。

For example, in my case, the values were (previously) respectively set at C:\Program Files (x86)\Java\jre7and C:\Program Files (x86)\Java\jre7\bin\client\jvm.dllwhich are nonexistent on my machine. I had to update these to the current folder and file of C:\Program Files (x86)\Java\jdk1.7.0_25\jreand C:\Program Files (x86)\Java\jdk1.7.0_25\jre\bin\client\jvm.dll.

例如,在我的情况下,这些值(以前)分别设置为C:\Program Files (x86)\Java\jre7C:\Program Files (x86)\Java\jre7\bin\client\jvm.dll在我的机器上不存在。我不得不将这些更新到当前文件夹和文件C:\Program Files (x86)\Java\jdk1.7.0_25\jreC:\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 Wow6432Nodefor 32 bit applications, in my case with the 32-bit JDK installed on a 64-bit machine).

同样,这将完全取决于您安装的 Java(JDK 和/或 JRE)版本——32 位或 64 位——以及您使用的操作系统类型——32 位或 64 位。只要知道它们反映在注册表中的不同位置(例如Wow6432Node32 位应用程序,在我的情况下,32 位 JDK 安装在 64 位机器上)。

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).

既然我已经更新了这两个注册表值,我的程序就可以完美地运行,不再有关于缺少 Java 运行时环境(源自注册表)的问题或抱怨。