Setting the target version of Java in ant javac

AntJavac

Ant Problem Overview


I need to compile a jar file using ant (1.7.0) to run under a specific version of Java (1.5). I currently have Java 1.6 on my machine. I have tried setting:

<target name="compile">
  <javac compiler="javac1.5" target="1.5" srcdir=.../>
</target>

I have also removed

<property name="build.compiler" value="modern"/>

and there is no properties file. I am running Java 1.6 on Linux/SUSE

Also is there a simple way of determining which version of Java is expected in the jar file.

Ant Solutions


Solution 1 - Ant

Use "target" attribute and remove the 'compiler' attribute. See here. So it should go something like this:

<target name="compile">
<javac target="1.5" srcdir=.../>
</target>

Hope this helps

Solution 2 - Ant

Both source and target should be specified. I recommend providing ant defaults, that way you do not need to specify source/target attribute for every javac task:

<property name="ant.build.javac.source" value="1.5"/>
<property name="ant.build.javac.target" value="1.5"/>

See Java cross-compiling notes for more information.

Solution 3 - Ant

To find the version of the java in the classfiles I used:

javap -verbose <classname>

which announces the version at the start as

minor version: 0
major version: 49

which corresponds to Java 1.5

Solution 4 - Ant

You may also set {{ant.build.javac.target=1.5}} ant property to update default target version of task. See http://ant.apache.org/manual/javacprops.html#target

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpeter.murray.rustView Question on Stackoverflow
Solution 1 - AntNawaManView Answer on Stackoverflow
Solution 2 - Antuser1338062View Answer on Stackoverflow
Solution 3 - Antpeter.murray.rustView Answer on Stackoverflow
Solution 4 - AntEugene PetrenkoView Answer on Stackoverflow