4

我正在尝试使用 Java 10、Ant 和 Eclipse 编译器编译这个简单的代码:

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>();
    }
}

这是我正在使用的 Ant 文件:

<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>

如果我使用 javac(compile-javac 目标),代码编译得很好,但我无法让它与 4.7.3a 或 4.8RC2 Eclipse 编译器一起工作:

  • 使用 4.7.3a,我得到错误参数化类型仅在源级别为 1.5 或更高时才可用,即使我指定release="10"
  • 在 4.7.3a 中,如果我使用source="10" and target="10"而不是release="10",则源级错误消失但我得到一个无效的模块名称:javax.xml.bind错误
  • 使用 4.8RC2,我得到源代码级别错误,并且另一个JAXBException 无法解析为类型错误,即使我指定我想添加定义 JAXBException 的 java.xml.bind 模块。

print-version-info 目标提供以下输出:

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

可能是ecj bug 487421的后续行动,还是我只是不理解命令行选项?

4

0 回答 0