peter.murray.rust
peter.murray.rust

Reputation: 38033

Setting the target version of Java in ant javac

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.

Upvotes: 74

Views: 154372

Answers (4)

NawaMan
NawaMan

Reputation: 25687

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>

Here's an example of using the javac command directly to achieve this. This information comes from @kon psych's link. I know and this doesn't show how did do this in an ant build.xml but it's still extremely helpful and I'd like to keep this information on the site in case that URL 404s some day.

Cross-Compilation Example

The following example uses javac to compile code that will run on a 1.6 VM.

% javac -source 1.6 -target 1.6 -bootclasspath jdk1.6.0/lib/rt.jar \
            -extdirs "" OldCode.java

The -source 1.6 option specifies that version 1.6 (or 6) of the Java programming language be used to compile OldCode.java. The option -target 1.6 option ensures that the generated class files will be compatible with 1.6 VMs. Note that in most cases, the value of the -target option is the value of the -source option; in this example, you can omit the -target option.

You must specify the -bootclasspath option to specify the correct version of the bootstrap classes (the rt.jar library). If not, the compiler generates a warning:

% javac -source 1.6 OldCode.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6

If you do not specify the correct version of bootstrap classes, the compiler will use the old language rules (in this example, it will use version 1.6 of the Java programming language) combined with the new bootstrap classes, which can result in class files that do not work on the older platform (in this case, Java SE 6) because reference to non-existent methods can get included.

Upvotes: 92

user1338062
user1338062

Reputation: 12735

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.

Upvotes: 51

Eugene Petrenko
Eugene Petrenko

Reputation: 4982

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

Upvotes: 5

peter.murray.rust
peter.murray.rust

Reputation: 38033

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

Upvotes: 16

Related Questions