简体   繁体   English

如何更改Eclipse / ANT的JAVA.HOME

[英]How to change JAVA.HOME for Eclipse/ANT

I am trying to sign a jar file using an ANT script. 我正在尝试使用ANT脚本签署一个jar文件。 I know this has to be pointed at the JDK directory for jarsigner.exe to run, but when I echo java.home it returns the JRE directory. 我知道必须指向JDK目录才能运行jarsigner.exe ,但是当我回显java.home时它会返回JRE目录。

This isn't a problem for javac, because I can set the executable path. 这对javac来说不是问题,因为我可以设置可执行路径。 But, that does not exist for signjar. 但是,signjar并不存在。

How do I change the java.home path? 如何更改java.home路径? When I right-click on MyComputer and go to: 当我右键单击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变量后,文件现在正确签名。

In Eclipse the Ant java.home variable is not based on the Windows JAVA_HOME environment variable. 在Eclipse中,Ant java.home变量不基于Windows JAVA_HOME环境变量。 Instead it is set to the home directory of the project's JRE. 相反,它被设置为项目的JRE的主目录。

To change the default JRE (eg 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->Properties and choose Java Build Path and choose the Libraries tab. 要仅更改单个项目的JRE,您可以转到Project-> Properties并选择Java Build Path并选择Libraries选项卡。 Find the JRE System Library and click it, then choose Edit and choose the JRE (or JDK) that you want. 找到JRE系统库并单击它,然后选择“ 编辑”并选择所需的JRE(或JDK)。

If that doesn't work then when running the build file you can choose Run as->Ant Build... and click the JRE tab, choose separate JRE and specify the JRE you want there. 如果这不起作用,那么在运行构建文件时,您可以选择Run as-> Ant Build ...并单击JRE选项卡,选择单独的JRE并指定您想要的JRE。

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. 对我来说,蚂蚁显然拒绝听取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.

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; ...并且你需要设置JAVA_HOME (它与你提到的PATH变量不同)来引用JDK主目录,而不是bin子目录; eg "C:\\program files\\java\\jdk". 例如“C:\\ program files \\ java \\ jdk”。

Simply, to enforce JAVA version to Ant in Eclipse: 简单地说,在Eclipse中将JAVA版本强制实施为Ant:

Use RunAs option on Ant file then select External Tool Configuration in JRE tab define your JDK/JRE version you want to use. 在Ant文件上使用RunAs选项,然后在JRE选项卡中选择外部工具配置 ,定义要使用的JDK / JRE版本。

Also be sure to set your JAVA_HOME environment variable. 还要确保设置JAVA_HOME环境变量。 In fact, I usually set the JAVA_HOME , then prepend the string " %JAVA_HOME%\\bin " to the system's PATH environment variable so that if Java ever gets upgraded or changed, only the JAVA_HOME variable will need to be changed. 实际上,我通常设置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. 并确保关闭任何命令提示符窗口或打开可能读取环境变量的应用程序,因为在重新启动应用程序之前通常不会注意到对环境变量的更改。

In addition to verifying that the executables are in your path, you should also make sure that Ant can find tools.jar in your JDK. 除了验证可执行文件在您的路径中之外,您还应确保Ant可以在tools.jar中找到tools.jar The easiest way to fix this is to add the tools.jar to the Ant classpath: 解决此问题的最简单方法是将tools.jar添加到Ant类路径:

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

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. 就像请求者一样,我忍受了每次提醒设置%JAVA_HOME%,每次看到这个非杰出的建议时都咬着我的舌头。 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. 最后,我思考我的笔记本电脑的JRE是否是我的JDK之前的版本(因为JRE会定期自动更新)并且我安装了最新的JDK。 The difference was minor, emanating from a matter of weeks of different versions. 差异很小,源于几个星期的不同版本。 I started with this error on jdk v 1.0865. 我开始在jdk v 1.0865上出现此错误。 The JRE was 1.0866. JRE是1.0866。 After installation, I had jdk v1.0874 and the equivalent JRE. 安装后,我有jdk v1.0874和等效的JRE。 At that point, I directed the Eclipse JRE to focus on my JDK and all was well. 那时,我指示Eclipse JRE专注于我的JDK,一切都很顺利。 My println of java.home even reflected the correct JRE. 我的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

If you are using Eclipse, try the following: 如果您使用的是Eclipse,请尝试以下操作:

  • Right click on the ant build file, then choose "Properties". 右键单击ant构建文件,然后选择“属性”。
  • 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. 单击“编辑”后,您将看到一个带有“属性”选项卡的新窗口,该选项卡将显示Ant构建属性的列表。 There is a "java.home" property in the list. 列表中有一个“java.home”属性。 Make sure it refers to the correct path. 确保它指的是正确的路径。

Go to Environment variable and add 转到Environment变量并添加

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

till jdk path (exclude bin folder) 直到jdk路径(不包括bin文件夹)
now set JAVA_HOME into path as PATH=%JAVA_HOME%\\bin; 现在将JAVA_HOME设置为路径为PATH=%JAVA_HOME%\\bin;
This will set java path to all the applications which are using java. 这将设置所有使用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%;

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. 对于Windows XP :要在Windows XP上设置环境变量,请右键单击“我的电脑”,然后选择“属性”。 Then go to the Advanced tab and click the Environment Variables button at the bottom. 然后转到“高级”选项卡,再单击底部的“环境变量”按钮。

For Windows 7 : To set environment variables on Windows 7, right click on Computer and select Properties. 对于Windows 7 :要在Windows 7上设置环境变量,请右键单击“计算机”,然后选择“属性”。 Click on Advanced System Settings and click the Environment Variables button at the bottom. 单击“高级系统设置”,然后单击底部的“环境变量”按钮。

The dialog for both Windows XP and Windows 7 is the same. Windows XP和Windows 7的对话框是相同的。 Make sure you're only working on system variables and not user variables. 确保您只处理系统变量而不是用户变量。

The only environment variable that you absolutely need is JAVA_HOME , which tells Ant the location of your JRE. 您绝对需要的唯一环境变量是JAVA_HOME ,它告诉Ant您的JRE的位置。 If you've installed the JDK, this is likely c:\\Program Files\\Java\\jdk1.xx\\jre on Windows XP and c:\\Program Files(x86)\\Java\\jdk1.xx\\jre on Windows 7. You'll note that both have spaces in their paths, which causes a problem. 如果你已经安装了JDK,这很可能是c:\\Program Files\\Java\\jdk1.xx\\jre在Windows XP和c:\\Program Files(x86)\\Java\\jdk1.xx\\jre在Windows 7你”请注意,它们的路径中都有空格,这会导致问题。 You need to use the mangled name[3] instead of the complete name. 您需要使用损坏的名称[3]而不是完整的名称。 So for Windows XP, use C:\\Progra~1\\Java\\jdk1.xx\\jre and for Windows 7, use C:\\Progra~2\\Java\\jdk1.6.0_26\\jre if it's installed in the Program Files(x86) folder (otherwise use the same as Windows XP). 所以对于Windows XP,使用C:\\Progra~1\\Java\\jdk1.xx\\jre ,对于Windows 7,如果它安装在Program Files(x86)中,请使用C:\\Progra~2\\Java\\jdk1.6.0_26\\jre )文件夹(否则使用与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 PATH variable. 只有这一点足以使Ant工作,但为方便起见,将Ant二进制路径添加到PATH变量是个好主意。 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. 为了能够在任何目录中运行ant,Windows需要知道ant二进制文件和java二进制文件的位置。 You'll need to add both of these to the end of the PATH variable. 您需要将这两者添加到PATH变量的末尾。 For Windows XP, you'll likely add something like this: 对于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM