如何为 Eclipse/ANT 更改 JAVA.HOME

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

How to change JAVA.HOME for Eclipse/ANT

javaeclipseantenvironment-variablesjava-home

提问by Ken

I am trying to sign a jar file using an ANT script. I know this has to be pointed at the JDK directory for jarsigner.exeto run, but when I echo java.home it returns the JRE directory.

我正在尝试使用 ANT 脚本对 jar 文件进行签名。我知道这必须指向 JDK 目录jarsigner.exe才能运行,但是当我回显 java.home 时它返回 JRE 目录。

This isn't a problem for javac, because I can set the executable path. But, that does not exist for signjar.

这对javac来说不是问题,因为我可以设置可执行路径。但是,signjar 不存在这种情况。

How do I change the java.home path? When I right-click on MyComputer and go to:

如何更改 java.home 路径?当我右键单击 MyComputer 并转到:

Properties > Advanced > Environment Variables

属性 > 高级 > 环境变量

The "PATH" variable is correctly pointed to the JDK (C:\program files\java\jdk\bin).

“PATH”变量正确指向 JDK ( C:\program files\java\jdk\bin)。

Update: The file now signs correctly after changing the PATH variable suggested below.

更新:更改下面建议的 PATH 变量后,文件现在可以正确签名。

回答by Adamski

Under Windows you need to follow:

在 Windows 下,您需要遵循:

Start -> Control Panel -> System -> Advanced -> Environment Variables.

开始 -> 控制面板 -> 系统 -> 高级 -> 环境变量。

... and you need to set JAVA_HOME(which is distinct from the PATH variable you mention) to reference the JDK home directory, not the bin sub-directory; e.g. "C:\program files\java\jdk".

...并且您需要设置JAVA_HOME(与您提到的 PATH 变量不同)以引用 JDK 主目录,而不是 bin 子目录;例如“C:\program files\java\jdk”。

回答by Adam Batkin

Also be sure to set your JAVA_HOMEenvironment variable. In fact, I usually set the JAVA_HOME, then prepend the string "%JAVA_HOME%\bin" to the system's PATHenvironment variable so that if Java ever gets upgraded or changed, only the JAVA_HOMEvariable will need to be changed.

另外一定要设置你的JAVA_HOME环境变量。事实上,我通常设置JAVA_HOME,然后%JAVA_HOME%\bin在系统的PATH环境变量前面加上字符串“ ”,这样如果 Java 升级或更改,只JAVA_HOME需要更改变量。

And make sure that you close any command prompt windows or open applications that may read your environment variables, as changes to environment variables are normally not noticed until an application is re-launched.

并确保关闭任何命令提示符窗口或打开可能读取环境变量的应用程序,因为在重新启动应用程序之前通常不会注意到环境变量的更改。

回答by Pace

In Eclipse the Ant java.homevariable is not based on the Windows JAVA_HOME environment variable. Instead it is set to the home directory of the project's JRE.

在 Eclipse 中,Ant java.home变量不是基于 Windows JAVA_HOME 环境变量。相反,它被设置为项目 JRE 的主目录。

To change the default JRE (e.g. change it to a JDK) you can go to Windows->Preferences...and choose Java->Installed JREs.

要更改默认 JRE(例如将其更改为 JDK),您可以转到Windows->Preferences...并选择Java->Installed JREs

To change just a single project's JRE you can go to Project->Propertiesand choose Java Build Pathand choose the Librariestab. Find the JRE System Libraryand click it, then choose Editand choose the JRE (or JDK) that you want.

要仅更改单个项目的 JRE,您可以转到Project->Properties并选择Java Build Path并选择Libraries选项卡。找到JRE 系统库并单击它,然后选择编辑并选择所需的 JRE(或 JDK)。

If that doesn't work then when running the build file you can choose Run as->Ant Build...and click the JREtab, choose separate JREand specify the JRE you want there.

如果这不起作用,那么在运行构建文件时,您可以选择Run as->Ant Build...并单击JRE选项卡,选择单独的 JRE并在那里指定您想要的 JRE。

回答by stolsvik

For me, ant apparently refuses to listen to any configuration for eclipse default, project JDK, and the suggestion of "Ant Home Entries" just didn't have traction - there was nothing there referring to JDK.

对我来说,ant 显然拒绝听取 eclipse 默认、项目 JDK 的任何配置,并且“Ant Home Entries”的建议没有吸引力 - 那里没有任何涉及 JDK 的内容。

However, this works:

但是,这有效:

Menu "Run" -> "External Tools" -> "External Tools Configuration".
  Goto the node "Ant build", choose the ant buildfile in question.
     Choose tab "JRE".
        Select e.g. "Run in same JRE as workspace", or whatever you want.

回答by ezzadeen

If you are using Eclipse, try the following:

如果您使用的是 Eclipse,请尝试以下操作:

  • Right click on the ant build file, then choose "Properties".
  • Click on the "Run/Debug Settings", then click on the launch configuration file. You should be able to edit it then.
  • After you click "Edit", you should see a new window with a "Properties" tab which will show you a list of Ant build properties. There is a "java.home" property in the list. Make sure it refers to the correct path.
  • 右键单击 ant 构建文件,然后选择“属性”。
  • 单击“运行/调试设置”,然后单击启动配置文件。然后你应该可以编辑它。
  • 单击“编辑”后,您应该会看到一个带有“属性”选项卡的新窗口,其中将显示 Ant 构建属性的列表。列表中有一个“java.home”属性。确保它指向正确的路径。

回答by Mahran ALSHIEKH

Simply, to enforce JAVA version to Ant in Eclipse:

简单地说,要在 Eclipse 中对 Ant 强制执行 JAVA 版本:

Use RunAs option on Ant file then select External Tool Configurationin JREtab define your JDK/JRE version you want to use.

在 Ant 文件上使用 RunAs 选项,然后在JRE选项卡中选择外部工具配置,定义要使用的 JDK/JRE 版本。

回答by AKD

Go to Environment variable and add

转到环境变量并添加

JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_37

till jdk path (exclude bin folder)
now set JAVA_HOME into path as PATH=%JAVA_HOME%\bin;
This will set java path to all the applications which are using java.

直到 jdk 路径(排除 bin 文件夹)
现在将 JAVA_HOME 设置为路径,因为PATH=%JAVA_HOME%\bin;
这会将 java 路径设置为所有使用 java 的应用程序。

For ANT use,

对于 ANT 使用,

ANT_HOME=C:\Program Files (x86)\apache-ant-1.8.2\bin;

and include ANT_HOME into PATH, so path will look like PATH=%JAVA_HOME%\bin;%ANT_HOME%;

并将 ANT_HOME 包含到 PATH 中,因此路径将如下所示 PATH=%JAVA_HOME%\bin;%ANT_HOME%;

回答by Shuvo Biswas

Set environment variables

设置环境变量

This is the part that I always forget. Because you're installing Ant by hand, you also need to deal with setting environment variables by hand.

这是我总是忘记的部分。因为您是手动安装 Ant,所以您还需要手动处理设置环境变量。

For Windows XP: To set environment variables on Windows XP, right click on My Computer and select Properties. Then go to the Advanced tab and click the Environment Variables button at the bottom.

对于 Windows XP:要在 Windows XP 上设置环境变量,请右键单击我的电脑并选择属性。然后转到“高级”选项卡并单击底部的“环境变量”按钮。

For Windows 7: To set environment variables on Windows 7, right click on Computer and select Properties. Click on Advanced System Settings and click the Environment Variables button at the bottom.

对于 Windows 7:要在 Windows 7 上设置环境变量,请右键单击计算机并选择属性。单击高级系统设置,然后单击底部的环境变量按钮。

The dialog for both Windows XP and Windows 7 is the same. Make sure you're only working on system variables and not user variables.

Windows XP 和 Windows 7 的对话框是相同的。确保您只处理系统变量而不是用户变量。

The only environment variable that you absolutely need is JAVA_HOME, which tells Ant the location of your JRE. If you've installed the JDK, this is likely c:\Program Files\Java\jdk1.x.x\jreon Windows XP and c:\Program Files(x86)\Java\jdk1.x.x\jreon Windows 7. You'll note that both have spaces in their paths, which causes a problem. You need to use the mangled name[3] instead of the complete name. So for Windows XP, use C:\Progra~1\Java\jdk1.x.x\jreand for Windows 7, use C:\Progra~2\Java\jdk1.6.0_26\jreif it's installed in the Program Files(x86) folder (otherwise use the same as Windows XP).

您绝对需要的唯一环境变量是JAVA_HOME,它告诉 Ant 您的 JRE 的位置。如果您已经安装了 JDK,这很可能c:\Program Files\Java\jdk1.x.x\jre在 Windows XP 和c:\Program Files(x86)\Java\jdk1.x.x\jreWindows 7 上。您会注意到它们的路径中都有空格,这会导致问题。您需要使用损坏的名称 [3] 而不是完整名称。因此,对于 Windows XP,请使用C:\Progra~1\Java\jdk1.x.x\jre,对于 Windows 7,C:\Progra~2\Java\jdk1.6.0_26\jre如果它安装在 Program Files(x86) 文件夹中,请使用(否则使用与 Windows XP 相同的方法)。

That alone is enough to get Ant to work, but for convenience, it's a good idea to add the Ant binary path to the PATHvariable. This variable is a semicolon-delimited list of directories to search for executables. To be able to run ant in any directory, Windows needs to know both the location for the ant binary and for the java binary. You'll need to add both of these to the end of the PATHvariable. For Windows XP, you'll likely add something like this:

仅此一项就足以让 Ant 工作,但为了方便起见,将 Ant 二进制路径添加到PATH变量是个好主意。此变量是以分号分隔的目录列表,用于搜索可执行文件。为了能够在任何目录中运行 ant,Windows 需要知道 ant 二进制文件和 java 二进制文件的位置。您需要将这两个添加到PATH变量的末尾。对于 Windows XP,您可能会添加如下内容:

;c:\java\ant\bin;C:\Progra~1\Java\jdk1.x.x\jre\bin

For Windows 7, it will look something like this:

对于 Windows 7,它看起来像这样:

;c:\java\ant\bin;C:\Progra~2\Java\jdk1.x.x\jre\bin

Done

完毕

Once you've done that and applied the changes, you'll need to open a new command prompt to see if the variables are set properly. You should be able to simply run ant and see something like this:

完成此操作并应用更改后,您需要打开一个新的命令提示符以查看变量设置是否正确。您应该能够简单地运行 ant 并看到如下内容:

Buildfile: build.xml does not exist!
Build failed

回答by Shane

Spent a few hours facing this issue this morning. I am likely to be the least technical person on these forums. Like the requester, I endured every reminder to set %JAVA_HOME%, biting my tongue each time I saw this non luminary advice. Finally I pondered whether my laptop's JRE was versions ahead of my JDK (as JREs are regularly updated automatically) and I installed the latest JDK. The difference was minor, emanating from a matter of weeks of different versions. I started with this error on jdk v 1.0865. The JRE was 1.0866. After installation, I had jdk v1.0874 and the equivalent JRE. At that point, I directed the Eclipse JRE to focus on my JDK and all was well. My println of java.home even reflected the correct JRE.

今天早上花了几个小时来面对这个问题。我可能是这些论坛上技术含量最低的人。和请求者一样,我忍受着每次设置 %JAVA_HOME% 的提醒,每次看到这个非杰出建议时都咬牙切齿。最后,我考虑了我的笔记本电脑的 JRE 是否比我的 JDK 版本早(因为 JRE 会定期自动更新),然后我安装了最新的 JDK。差异很小,源于几个星期的不同版本。我从 jdk v 1.0865 上的这个错误开始。JRE 为 1.0866。安装后,我有 jdk v1.0874 和等效的 JRE。那时,我指示 Eclipse JRE 专注于我的 JDK,一切都很好。我的 java.home 的 println 甚至反映了正确的 JRE。

So much feedback repeated the wrong responses. I would strongly request that people read the feedback from others to avoid useless redundancy. Take care all, SG

如此多的反馈重复了错误的反应。我强烈要求人们阅读其他人的反馈,以避免无用的冗余。照顾好一切,SG

回答by vallismortis

In addition to verifying that the executables are in your path, you should also make sure that Ant can find tools.jarin your JDK. The easiest way to fix this is to add the tools.jarto the Ant classpath:

除了验证可执行文件是否在您的路径中之外,您还应该确保 Ant 可以tools.jar在您的 JDK 中找到。解决此问题的最简单方法是将tools.jar加到 Ant 类路径中:

Adding tools.jar to Ant classpath.

将 tools.jar 添加到 Ant 类路径。