How to compile this code with Java 10, Ant and the Eclipse compiler?

3.2k views Asked by At

I am trying to compile this simple code using Java 10, Ant and the Eclipse compiler:

import java.util.ArrayList;
import javax.xml.bind.JAXBException;

class Test {
    void foo() throws JAXBException {
        throw new JAXBException("hi there");
    }

    void bar() {
        new ArrayList<String>();
    }
}

This is the Ant file I am using:

<project name="Java 10 test">

    <target name="compile-javac" depends="clean, print-version-info">
        <javac release="10" includeantruntime="false">
            <src path="."/>
            <compilerarg value="--add-modules"/>
            <compilerarg value="java.xml.bind"/>
        </javac>
    </target>

    <target name="compile-ecj-4.7" depends="clean, print-version-info">
        <javac compiler="org.eclipse.jdt.core.JDTCompilerAdapter"
            release="10" includeantruntime="false">
            <src path="."/>
            <compilerclasspath>
                <pathelement path="ecj-4.7.3a.jar"/>
            </compilerclasspath>
            <compilerarg value="--add-modules"/>
            <compilerarg value="java.xml.bind"/>
        </javac>
    </target>

    <target name="compile-ecj-4.8" depends="clean, print-version-info">
        <javac compiler="org.eclipse.jdt.core.JDTCompilerAdapter"
            release="10" includeantruntime="false">
            <src path="."/>
            <compilerclasspath>
                <pathelement path="ecj-4.8RC2.jar"/>
            </compilerclasspath>
            <compilerarg value="--add-modules"/>
            <compilerarg value="java.xml.bind"/>
        </javac>
    </target>

    <target name="clean">
        <delete file="Test.class"/>
    </target>

    <target name="print-version-info">
        <echo message="Java home is ${java.home}"/>
        <echo message="Java version is ${java.version}"/>
        <echo message="Ant version is ${ant.version}"/>
    </target>

</project>

The code compiles fine if I'm using javac (compile-javac target) but I can't get it to work with either the 4.7.3a or 4.8RC2 Eclipse compilers:

  • with 4.7.3a, I get the error parameterized types are only available if source level is 1.5 or greater even though I specify release="10"
  • with 4.7.3a, if I use source="10" and target="10" instead of release="10", the source level error disappears but I get a invalid module name: javax.xml.bind error
  • with 4.8RC2, I get the source level error and another JAXBException cannot be resolved to a type error, even though I specify that I would like to add the java.xml.bind module where JAXBException is defined.

The print-version-info target gives the following as output:

print-version-info:
     [echo] Java home is C:\Program Files\Java\jdk-10
     [echo] Java version is 10
     [echo] Ant version is Apache Ant(TM) version 1.10.3 compiled on March 24 2018

May be it is a follow up for ecj bug 487421 or I just don't understand the command line options?

0

There are 0 answers