【问题标题】:AntBuilder works in Groovy Console but not in EclipseAntBuilder 在 Groovy 控制台中有效,但在 Eclipse 中无效
【发布时间】:2026-01-22 13:10:02
【问题描述】:

我运行的是 Windows 7。以下代码在 Groovy 控制台中运行:

def fileEx = new File(/Y:\Documents\Test File.txt/)
def copyToFile = new File(/Y:\Documents\Test File - Copied by Groovy and Ant.txt/)

ant = new AntBuilder()
ant.copy(file:fileEx, tofile:copyToFile)

但是当我尝试在 Eclipse 中将它作为 Groovy 脚本运行时,我得到了这个错误:

Caught: java.lang.NoClassDefFoundError: org/apache/tools/ant/BuildException
java.lang.NoClassDefFoundError: org/apache/tools/ant/BuildException
    at copyFile.run(copyFile.groovy:4)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.BuildException

我尝试将 ant.jar 添加到 myclasspath 中,包括 Groovy lib 文件夹中的 ant.jar 和 Eclipse plugins lib 文件夹中的 ant.jar,但这并不能解决问题。

【问题讨论】:

    标签: eclipse groovy


    【解决方案1】:

    看起来groovy-eclipse plugin 附带了一个完整安装 groovy 的库子集。除了 eclipse 插件之外,您还需要完整安装 groovy,您可能已经拥有它。

    您应该能够通过转到您的项目并转到 Properties->Java Build Path,然后单击“Libraries”来解决您的问题。 “添加外部罐子”并导航到完整的 groovy 安装。在那里,lib 目录应该有 ant.jar 和 ant-launcher.jar。将它们都添加。这应该可以解决这个问题(并将潜在问题排队等待调试)。

    【讨论】:

    • 非常感谢。这解决了我的问题。实际上,我将它们作为外部 jar 添加到已安装的 JRE 中,因此它们可用于我的所有项目。
    最近更新 更多