Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant buildfile setting javac location

Tags:

java

xml

build

ant

I'm editing the buildfile of an old project. When I add some jar files to the project that use Java 1.6, it won't build. It says

[javac] javac: invalid target release: 1.6

So clearly I need to tell the ant buildfile to use javac 1.6.

How do I do this? I have JDK1.6 installed on my system, but the default javac is 1.5. I don't want to change the default javac... I just want to set the javac location in this one proejct to JDK1.6/bin/javac. How do I do this in the XML of an ant buildfile?

Thanks,
ktm

like image 791
ktm5124 Avatar asked Feb 01 '11 16:02

ktm5124


People also ask

Where does javac compile to?

By default, javac compiles each source file to a class file in the same directory as the source file. However, it is recommended to specify a separate destination directory with the -d option described in Standard Options.

Where is build xml in Ant?

Typically, Ant's build file, called build. xml should reside in the base directory of the project.

Which Ant task does the Java compiling?

Ant Javac task is used to compile Java source file. It scans source and destination directory to compile the source file. It only compiles if either . class is not present or .

What is the default target in Ant?

As Ant runs, it displays the name of each target executed. As our example output shows, Ant executes prepare followed by compile . This is because compile is the default target, which has a dependency on the prepare target.


2 Answers

This was pulled from the javac task documentation

<javac srcdir="" 
         destdir=""
         executable="path-to-java16-home/bin/javac" 
         fork="true"
         taskname="javac1.6" />
like image 186
Aaron McIver Avatar answered Sep 24 '22 02:09

Aaron McIver


You can create a wrapper script in which you change your JAVA_HOME before executing ant.

e.g. build.sh

export JAVA_HOME=/path/to/jdk1.6
ant "$@"

Now when you call build.sh it will use java6 for both javac, java and any other commands.

IMO this is better than fiddling with the build.xml (and forking a new javac process).

like image 36
dogbane Avatar answered Sep 27 '22 02:09

dogbane