在 NetBeans(NetBeans)中構建項目時,程式本身沒有語法錯誤,卻在編譯階段報出 build-impl.xml:305: Compile failed,讓不少開發者摸不著頭腦。本文分析這個錯誤的成因與解決辦法。
報錯信息通常長這樣:
build-impl.xml:868: The following error occurred while executing this line:
build-impl.xml:305: Compile failed; see the compiler error output for details.
定位到 305 行,會發現它是 javac 任務的定義:
<javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}"
encoding="${source.encoding}" excludes="@{excludes}" executable="${platform.javac}"
fork="yes" includeantruntime="false" includes="@{includes}" source="${javac.source}"
srcdir="@{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
真正的元兇往往不是這一行本身,而是它內部這段注解處理器相關的 compilerarg 配置:
<compilerarg line="${endorsed.classpath.cmd.line.arg}"/>
<compilerarg line="${javac.compilerargs}"/>
<compilerarg value="-processorpath"/>
<compilerarg path="@{processorpath}:${empty.dir}"/>
<-\- ... 其餘 compilerarg 與 customize 配置 ... -->
把這一段 compilerarg(包含 -processorpath、-s 等注解處理相關參數)刪除,再重新構建,通常就能成功。這類問題多與項目中引入的注解處理器或環境變量配置有關,移除多餘的編譯器參數即可繞過。