aRise
aRise

Reputation: 139

Cross compilation : jdk1.6 on jdk 1.7

I Have a java source code written on jdk7. and have jdk7 and jre7 on my machine.

Now i need to compile this code with compiler jdk1.6 using ANT.

So I tried to add the following lines in my project build.xml.

<target name="compile">
<javac target="1.6" srcdir="src"/>
</target>

Is this enough ? Or do i have to add bootclasspath? If yes, please specify syntax with which i can add in build file

Upvotes: 1

Views: 1699

Answers (1)

Jesper
Jesper

Reputation: 206776

That might not be enough.

First of all, besides setting target to 1.6, you should also set source to 1.6:

<javac source="1.6" target="1.6" srcdir="src">

to let the Java compiler know that it should interpret your code as 1.6-compatible source code.

But a bigger problem is that this does not prevent you from using classes, interfaces and methods which are new in the Java 7 standard library. If you use Java 7-only classes, interfaces or methods, it will compile, but you'll get errors like NoSuchMethodError when you run it on Java 6 and you're calling a method that didn't exist in Java 6.

The safest way to ensure that your code is compatible with Java 6 is installing JDK 6 and using that to compile your code. (Note: There's no problem with having multiple versions of the JDK installed on your machine, just install them in different directories).

Upvotes: 3

Related Questions