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

我该如何解决这个问题?


当前回答

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

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

-Djava.home=YOUR_PATH_HERE

或者当你执行Java时,你可以在命令中添加命令行开关:

java -Djava.home=PATH SomeJavaApp

其他回答

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.

用例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 vs. Path

如果您的问题与编译Java有关,那么您需要检查JAVA_HOME变量和Path(如果适用)。如果您的问题与运行Java应用程序有关,那么您需要检查Path变量。

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.

符号链接

符号链接可以提供另一种引用JDK搜索路径的方法,即通过装载现有的环境变量之一。

我不确定以前版本的Oracle/Sun JDK/JRE版本,但至少jdk1.8.0_74的安装程序将搜索路径C:\ProgramData\Oracle\Java\javapath附加到path变量,并将其放在字符串值的开头。此目录包含到JRE目录中的java.exe、javaw.exe和javaw.exe的符号链接。

因此,至少对于Java 8 JDK,以及独立的Java 8 JRE,不需要为JRE进行环境变量配置。只要你使用安装包来设置它。不过,Windows安装可能会有不同。注意,Oracle JRE是与JDK捆绑在一起的。

如果您发现您的Java JDK配置使用了错误的编译器版本,或者它看起来像魔术一样工作,而没有显式地定义(没有施咒),那么您的环境变量中可能有一个符号链接。你可能需要检查符号链接。

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

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

echo %JAVA_HOME%

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

echo %PATH%

查找JDK安装目录

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

打开JDK默认安装路径:

C:\Program Files\Java

应该有这样的子目录:

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

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


设置JAVA_HOME变量

有了JDK安装路径后:

右键单击桌面上的“我的电脑”图标,选择“属性”。 单击高级选项卡,然后单击环境变量按钮。 在“系统变量”下单击“新建”。 输入变量名JAVA_HOME。 输入变量值作为Java Development Kit的安装路径。 单击OK。 单击“应用更改”。

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

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

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

以管理员身份运行Eclipse。

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