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

自作多情 提交于 2019-12-10 15:28:36

问题


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?

来源:https://stackoverflow.com/questions/50683168/how-to-compile-this-code-with-java-10-ant-and-the-eclipse-compiler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!