【问题标题】:Run ant script from command,error happened:Class not found: org.eclipse.jdt.core.JDTCompilerAdapter从命令运行 ant 脚本,发生错误:找不到类:org.eclipse.jdt.core.JDTCompilerAdapter
【发布时间】:2012-05-17 13:23:32
【问题描述】:

在我的java web项目中,有<T>之类的代码,在ant脚本中,javac使用JDK编译java代码,编译不成功。

后来,我知道它必须使用eclipse JDT来编译。

而且,在eclipse中,ant脚本可以运行成功。当这样运行时:

右键点击build.xml ---> Run ---> Run as ---> External Tools Configurations,点击JRE,选择“Run in the same JRE as the workspace”。

之后,ant就可以运行成功了,在eclipse中。

但是,我想编写一个 .bat 和 .sh 文件来调用 ant 脚本,来编译、war、部署和启动 Tomcat。 所以,蚂蚁应该从命令运行。我尝试了更多,总是发生错误: 找不到类:org.eclipse.jdt.core.JDTCompilerAdapter

PS,我已经将eclipse插件中JDT的jar文件复制到ant_home/lib目录下。

希望得到您的回应。提前致谢!

构建.xml

`

<tstamp>
    <format property="build.time" pattern="yyyy-MM-dd" />
</tstamp>

<path id="project.classpath">
    <fileset dir="${lib.dir}">
        <include name="**/*.jar" />
    </fileset>
    <fileset dir="${catalina.home}/lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${ant.dir}">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="clear">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
    <delete file="${catalina.home}/webapps/${webapp.name}.war" />
    <delete dir="${catalina.home}/webapps/${webapp.name}" />
</target>

<target name="init" depends="clear">
    <mkdir dir="${build.dir}/classes" />
    <mkdir dir="${dist.dir}" />
</target>

<target name="compile" depends="init">
    <echo message="begin compile..." />
    <javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
        includeantruntime="false" nowarn="on" 
        source="1.6" target="1.6" deprecation="true" debug="true" 
        encoding="UTF-8" classpathref="project.classpath">
        <compilerarg line="-Xlint:unchecked" />
    </javac>
    <copy todir="${build.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.xml" />
            <include name="**/*.properties" />
            <include name="**/*.sql" />
        </fileset>
        <fileset dir="${config.dir}">
            <include name="**/*.xml" />
            <include name="**/*.properties" />
            <include name="**/*.sql" />
        </fileset>
    </copy>
    <echo message="end compile..." />
</target>

<target name="war" depends="compile">
    <echo message="begin war..." />
    <war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
        webxml="${webRoot.dir}/WEB-INF/web.xml">
        <lib dir="${lib.dir}" />
        <classes dir="${build.dir}/classes" />
        <fileset dir="${webRoot.dir}">
            <include name="***.*" />
        </fileset>
    </war>
    <echo message="end war..." />
</target>

<target name="deploy" depends="war">
    <echo message="begin deploy..." />
    <copy file="${dist.dir}/${webapp.name}.war"    todir="${catalina.home}/webapps" />
    <echo message="end deploy..." />
</target>

</project>

`

【问题讨论】:

  • 上面的脚本遗漏了很多。&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project name="lmsx" default="deploy" basedir="D:/workspace/lms"&gt; &lt;property environment="env" /&gt; &lt;property name="webapp.name" value="lms" /&gt; &lt;property name="catalina.home" value="D:/developer/apache-tomcat-6.0.32" /&gt; &lt;property name="dist.dir" value="${basedir}/dist" /&gt; &lt;property name="ant.dir" value="D:/developer/apache-ant-1.8.3" /&gt;
  • ` `

标签: java ant eclipse-jdt


【解决方案1】:

作为记录,当在 &lt;parallel&gt; 上下文中(即在多线程情况下)将 &lt;javac&gt; 任务与该编译器适配器一起使用时,我也会随机收到此错误(经常发生)。

看起来编译器适配器jar被暂时锁定,线程类加载器或其他东西无法访问。除了删除 parallel> 执行之外,我还没有解决方法。

【讨论】:

    【解决方案2】:

    从 Eclipse 下载 ecj*.jar 并放在 ANT_HOME/lib 下。 确保在 shell 环境下设置了 ANT_HOME,或者你应该在 shell 的 CLASSPATH 中设置 ecj*.jar。 (否则,可能仍会抛出 Class not found: org.eclipse.jdt.core.JDTCompilerAdapter。)

    【讨论】:

      【解决方案3】:

      不要将 Eclipse IDE 中的 ant 用于命令行。

      单独下载 ant 并将其解压缩到类似 - C:\apache\ant - 对于 windows 的位置,并将其 bin 目录放在您的 PATH 中。它会附带一些需要添加到 CLASSPATH 中的 jars。

      对于 Mac OSX 的“sudo port install ant”会处理一切。

      【讨论】:

      • 我还是不明白你为什么要强制使用eclipse ant编译。如果你想从 Eclipse 外部运行它,你需要独立的 ant。从 Eclipse 中包含一些您不知道的 jars。当您将其作为 Eclipse 插件运行时,尝试打印类路径并查看所有 jar 文件都在使用什么。没有必要依赖基于 Eclipse 的 jar。您也许可以找到适用于所有情况的独立 jar。
      • 签出 - javalobby.org/java/forums/t71033.htmlblog.andrewbeacock.com/2005/08/… ... 并删除“eclipse 插件中有关 JDT 的 jar 文件到 ant_home/lib 目录”。能否再描述一下如果不使用 JDT 编译器会出现什么错误?
      • 此链接可能有效 - 基本上看起来 eclipse 编译器和 JDK 编译器之间存在一些差异 - stackoverflow.com/questions/1609531/…
      • 下载 ecj.jar 并按照 help.eclipse.org/galileo/index.jsp?topic=/… 将其放在您的类路径中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2012-04-12
      • 2014-01-05
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多