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

我该如何解决这个问题?


当前回答

你可能想在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,不需要安装程序,这样您就可以随意使用它。

其他回答

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

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

你可能想在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,不需要安装程序,这样您就可以随意使用它。

您必须首先在您的系统中安装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;[带分号的附加值]

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

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

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