在Eclipse中调用它时出现Ant错误



我已经尝试过以编程方式调用Ant,但是我遇到了这个错误

线程"main"异常java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain

我尝试使用控制台单独运行build.xml,并通过在eclipse中右键单击它作为ant build运行。运行没有问题。

我的示例蚂蚁测试类

public class AntTest {
    public static void main(String[] args) {
        File buildFile = new File("build.xml");
        Project p = new Project();
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.executeTarget(p.getDefaultTarget());
    }
}

我的示例build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="testproject" default="test" basedir=".">
    <target name="test">
        <echo message="Hello World" />
    </target>
</project>

我错过了什么?

确保ant库位于您的类路径中。
看起来,ant-launcher-VERSION.jar(其中版本是您正在使用的ant版本)在您的类路径中缺失。

最新更新