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

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

我该如何解决这个问题?


当前回答

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

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


例子

Windows 7 Select Computer from the Start menu Choose System Properties from the context menu Click Advanced system settings -> Advanced tab Click on Environment Variables, under System Variables, find PATH, and click on it. 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. Reopen Command prompt window, and run your Java code.

其他回答

在将Java目录添加到PATH变量时,您可能希望将其放在PATH变量的开头。我有一个问题,将Java目录放在PATH的末尾将不起作用。经过检查,我在我的Windows\System32目录中找到了java.exe,看起来第一个赢了,当你的PATH中有几个同名的文件时…

Eclipse: Window->Preferences->Java->已安装的jre

使用搜索功能确保列出了最新的Java安装;然后确保它是被检查的那个。这应该是一个JDK,而不是JRE。

以管理员身份运行Eclipse。

这解决了我的问题。我还在研究背后的逻辑。

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

设置Java主页

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

有了JDK安装路径后:

右键单击“我的电脑”图标 选择Properties。 单击屏幕左侧的高级系统设置选项卡 一个预先弹出窗口打开。 点击环境变量按钮。

在“系统变量”下单击“新建”。 输入变量名JAVA_HOME。 输入变量值作为Java Development Kit的安装路径。 单击OK。 单击“应用更改”。

在“system variable”下设置“JAVA Path”

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

你可能想在Windows/System32文件夹中找到以下文件: ——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.

这也意味着最好手动安装Java,不需要安装程序,这样您就可以随意使用它。