How to change JAVA.HOME for Eclipse/ANT
Asked Answered
T

10

50

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.exe to run, but when I echo java.home it returns the JRE directory.

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

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

Properties > Advanced > Environment Variables

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

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

Totipalmate answered 17/8, 2009 at 14:43 Comment(0)
T
57

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

To change the default JRE (e.g. change it to a JDK) you can go to Windows->Preferences... and choose 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. Find the JRE System Library and click it, then choose Edit and choose the JRE (or JDK) that you want.

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.

Trovillion answered 14/12, 2010 at 14:32 Comment(4)
This doesn't work for me - tests run from within ant are still having eclipse's java environment. (I run eclipse with my default java, which is 1.8 ea. But the project cannot run with 1.8 (due to some ASM stuff not being compatible with the 1.8 compiled classes) - but ant refuses to listen to Eclipse's default JDK nor the project JDK.. In Ant Home Entries, there are nothing that points to a JDK.Notorious
So I'm a bit confused. Your first sentence says that "tests run from Ant are inheriting Eclipse's Java environment" but you end with "Ant refuses to listen to Eclipse's JDK". If you want Eclipse to run your project with 1.8 but Ant to run with 1.7 then it seems you would set the project's JRE to 1.8 (second approach listed) and then specify an alternate JRE in the Ant build configuration (third approach listed). I have no idea what Ant home entries is either, I'll see if I can remove that.Trovillion
This didn't affect Ant's JDK choice for me, had to change the settings in stolsvik's answerRothstein
This worked for me, but because I had tried launching before I changed the default JRE, I had to also delete the associated launch file in <workspace_path>\.metadata\.plugins\org.eclipse.debug.core\.launchesSemiotics
N
19

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.

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.
Notorious answered 5/6, 2013 at 15:11 Comment(0)
A
14

Under Windows you need to follow:

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".

Aleksandrovsk answered 17/8, 2009 at 14:50 Comment(1)
Changing that variable made it so the script doesnt crash so thank you there. Update above - SF and DSA files are not being placed in the JARTotipalmate
D
12

Simply, to enforce JAVA version to Ant in Eclipse:

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

Desman answered 19/11, 2013 at 12:16 Comment(0)
C
4

Also be sure to set your JAVA_HOME environment variable. 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.

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.

Characterize answered 17/8, 2009 at 14:51 Comment(1)
Thanks the file does sign now, but under the META-INF folder I do not get a SF or DSA file that I get when I manually sign the jar. WebStart still says unsigned entries. Anything special I have to do for those to appear or are those needed?Totipalmate
N
4

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. The easiest way to fix this is to add the tools.jar to the Ant classpath:

Adding tools.jar to Ant classpath.

Nefertiti answered 2/11, 2017 at 20:54 Comment(2)
This worked for me although i had to still add java.home property to the property tab of this window .Oliviero
This also correctly solves the ANT compile issue of not finding the JDK; the error stating the JAVA_HOME variable may not be set properly.Kyte
T
1

If you are using Eclipse, try the following:

  • 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.
Throat answered 27/8, 2013 at 21:6 Comment(0)
T
1

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.

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

Thermotensile answered 6/2, 2016 at 19:52 Comment(0)
M
0

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.

For ANT use,

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%;

Machicolate answered 13/5, 2014 at 5:40 Comment(0)
H
0

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.

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.

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.

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.

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\jre on Windows XP and c:\Program Files(x86)\Java\jdk1.x.x\jre on 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\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).

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. 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 PATH variable. For Windows XP, you’ll likely add something like this:

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

For Windows 7, it will look something like this:

;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:

Buildfile: build.xml does not exist!
Build failed
Holdback answered 1/8, 2014 at 6:27 Comment(1)
-1 If you're going to rip off someone else's work, the least you can do if give credit where it's due... nczonline.net/blog/2012/04/12/…Glacialist

© 2022 - 2024 — McMap. All rights reserved.