How to change JAVA.HOME for Eclipse/ANT

Solution 1:

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.

Solution 2:

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.

Solution 3:

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

Solution 4:

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.

Solution 5:

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.