我在控制面板的“系统”中的环境变量中创建了两个新变量,一个用于用户变量,一个用于系统变量。它们都被命名为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

我该如何解决这个问题?


当前回答

您需要将其设置为C:\Sun\SDK\jdk(假设这是jdk的安装位置-这不是默认值)-不要将\bin放在C:\Sun\SDK\jdk\bin中。

如果你的应用程序只在你作为当前用户登录时运行,那么把它放在用户变量中——如果它需要为系统上的所有用户运行,那么把它放在系统变量中。

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

其他回答

对我来说,有效的方法是将%JAVA_HOME%\bin添加到Path环境变量中,并将JAVA_HOME环境变量指向jdk文件夹。

这是从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.

您需要将其设置为C:\Sun\SDK\jdk(假设这是jdk的安装位置-这不是默认值)-不要将\bin放在C:\Sun\SDK\jdk\bin中。

如果你的应用程序只在你作为当前用户登录时运行,那么把它放在用户变量中——如果它需要为系统上的所有用户运行,那么把它放在系统变量中。

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

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

在cmd中(暂时用于该cmd窗口):

set JAVA_HOME="C:\\....\java\jdk1.x.y_zz"

echo %JAVA_HOME%

set PATH=%PATH%;%JAVA_HOME%\bin

echo %PATH%