Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting JAVA_HOME when running Ant from Java

Tags:

java

javac

ant

The reason is long and boring, but I need to run an Ant script to compile Java 1.5 code from a Java 1.4 app. I keep getting this error, though:

BUILD FAILED

build.xml:16: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\j2sdk1.4.2_16\jre"

In my code, I have:

Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.setProperty("java.home", "C:\Program Files\Java\jdk1.6.0_04");
p.fireBuildStarted();
p.init();
// so on and so forth

but it ignores it. I've also tried p.setUserProperty(String, String), but that doesn't do the trick, either. Is there a way to do it without launching a separate process?

like image 447
Adam Crume Avatar asked Mar 16 '09 20:03

Adam Crume


People also ask

How do you JAVA_HOME is set to the location of your JDK?

To set JAVA_HOME, do the following: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1.

Is JAVA_HOME automatically set?

In short, these export commands will automatically update JAVA_HOME variable as you re-install/upgrade your JDK/JRE packages or change default Java version. No need to adjust JAVA_HOME manually.

Should JAVA_HOME point to JDK or JRE?

If you're doing any sort of development, or building with Maven or Ant, you need to point to the JDK (Java Development Kit) where utilities such as javac (the Java Compiler) reside. Otherwise, you can point to the JRE (Java Runtime Environment). The JDK contains everything the JRE has and more.


1 Answers

Does the javac task in your buildfile have fork="yes"? If not, then it doesn't matter what the java.home property is set to; ant will attempt to call the javac Main method in the same java process, which from your error is a JRE, not a JDK.

EDIT Try setting the executable property of your javac task to the full path to the javac binary and add compiler="extJavac" to the task.

like image 159
Jason Day Avatar answered Oct 04 '22 07:10

Jason Day