ant在eclipse下出错

来源:互联网 发布:网络支付的规模 编辑:程序博客网 时间:2024/05/16 14:22

 学着使用Ant,可以在命令行或Eclipse下运行ant.编译build.xml文件。

具体学习可以看以下两个链接:

http://oryx.javaeye.com/blog/154498

http://addone.bokee.com/1389346.html

里面已经讲清楚了ant的使用和build.xml文件怎么写,但我自己在使用ant的时候确出现了一些问题。

首先我了解到Eclipse安装好之后,自己就带了ant插件,这个可以在Eclipse安装目录如我的在下面目录可以找到

C:/Program Files/eclipse3.3/plugins/org.apache.ant_1.7.0.v200706080842

也可以到网上下载

Eclipse各版本下载:http://archive.eclipse.org/eclipse/downloads/

单独下载ant:http://www.apache.org/在这个网站下点击ant链接即可到http://ant.apache.org/下载想要的ant版本。我下载了1.7.1版本的。ant配置很容易,只需在path环境变量中加入ant目录下的bin目录即可如C:/Java/ant1.7.1/bin。

然后在命令行使用ant,第一次会提示没有build.xml文件,我在eclipse里新建工程并建build.xml文件, 内容如下

  1. <project name="GetsTest" basedir="WebContent" default="Deploy_for_development">
  2.     <property name="web.app.name" value="GetsTest"/>
  3.     <property name="deployDir" value="c:/GeTSmart/jboss-3.2.0/server/minimal/deploy/${web.app.name}.war" />
  4.     <property name="name" location="location"/>
  5.     
  6.     <!-- ====================================
  7.          == target: Deploy_for_development ==
  8.          ==    for deploy to development   ==     
  9.          ====================================-->
  10.     <target name="Deploy_for_development" description="simple deploy for development">
  11.         <!-- clean the deploy directory -->
  12.         <echo message="clean the deploy directory"/>
  13.         <delete dir="${deployDir}"></delete>
  14.         
  15.         <!-- create the deploy directory -->
  16.         <echo message="create the deploy directory"/>
  17.         <mkdir dir="${deployDir}"/>
  18.         <!-- copy the source GetsTest to deployDir-->
  19.         <echo message="copy the source GetsTest to deployDir"/> 
  20.         <copy todir="${deployDir}" overwrite="true">
  21.             <fileset dir="." >
  22.                 <exclude name="**/web-temp.xml"/>
  23.                 <exclude name="**/eus.log.*"/>
  24.             </fileset>
  25.         </copy>
  26.     </target>
  27.     
  28. </project>

cd到build.xml文件所在目录直接使用ant即可,提示build successfully .

但下来就出问题了,因为我刚才是在命令行下运行的,注意使用的是我下载的ant1.7.1版本的没有问题。

在eclipse里对build.xml文件run as ant build 确出现如下的错误:

BUILD FAILED
java.lang.NoClassDefFoundError: org/xml/sax/SAXException
 at org.apache.tools.ant.ProjectHelper.getProjectHelper(ProjectHelper.java:228)
 at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.parseBuildFile(InternalAntRunner.java:189)
 at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:400)
 at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
为什么会出现这个错误,又怎么解决呢?

首先知道我在命令行使用的ant与在eclipse下使用的ant是不同的,因为我下载的ant是1.7.1版本的,而且path指定的也是这个版本的ant,编译出来是没有问题的。所以肯定是eclipse所使用的ant有问题,所以我把eclipse自带的ant拷贝出来,他在目录C:/Program Files/eclipse3.3/plugins/org.apache.ant_1.7.0.v200706080842下面,我把path指向这个ant所在的bin目录,并在命令行运行:

出错如下:

BUILD FAILED
java.lang.NoClassDefFoundError: org/xml/sax/SAXException
        at org.apache.tools.ant.ProjectHelper.getProjectHelper(ProjectHelper.java:228)
        at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:94)
        at org.apache.tools.ant.Main.runBuild(Main.java:683)
        at org.apache.tools.ant.Main.startAnt(Main.java:199)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

说明问题确实出在eclipse自带的ant上。这里说明一下我的jdk使用的是1.3版本的,查了一下这个版本的JDK确实没有org/xml/sax/SAXException这个类,那怎么解决呢?

在网上有相同的错误帖子如下:http://techsharp.blogspot.com/2008/04/eclipse-and-ant-with-jdk13.html

按照上面帖子说明的方法只要将eclipse自带的插件即C:/Program Files/eclipse3.3/plugins文件夹下的org.apache.xerces_2.8.0.v200705301630.jar加到ant home Entires即可。具体操作如下:

Eclipse(我的是3.3版本的),Windows-->preferences-->Ant-->runtime-->classpath-->Ant home Entires-->add External jar

选择eclipse目录下的jar文件如C:/Program Files/eclipse3.3/plugins/org.apache.xerces_2.8.0.v200705301630.jar,点确定后再在eclipse下点build.xml文件run as ant build:

结果如下:

Buildfile: D:/eclipse3.3/workspace/GetsTest1.0/build.xml
Deploy_for_development:
     [echo] clean the deploy directory
   [delete] Deleting directory c:/GeTSmart/jboss-3.2.0/server/minimal/deploy/GetsTest.war
     [echo] create the deploy directory
    [mkdir] Created dir: c:/GeTSmart/jboss-3.2.0/server/minimal/deploy/GetsTest.war
     [echo] copy the source GetsTest to deployDir
     [copy] Copying 4 files to c:/GeTSmart/jboss-3.2.0/server/minimal/deploy/GetsTest.war
     [copy] Copied 7 empty directories to 1 empty directory under c:/GeTSmart/jboss-3.2.0/server/minimal/deploy/GetsTest.war
BUILD SUCCESSFUL
Total time: 1 second

至此问题解决!

回想一下提示java.lang.NoClassDefFoundError: org/xml/sax/SAXException等错误是因为我的jdk版本使用的是1.3的,rt.jar里根本没有org.xml包,所以肯定会出错!那么解决办法1就是換jdk的版本,但会影响到我的程序运行,显然不行。2.就是想办法导入org.xml包,而插件org.apache.xerces_2.8.0.v200705301630.jar里面是包括org.xml包的,所以导入进来是可以解决些问题的。

 

在网上也看到类似的问题但解决的方法不够详细,而我刚好使用的环境jdk1.3,eclipse 3.3恰恰碰上了这个问题,所以结合起来解之。。。。

 

这里eclipse3.3的版本的ant.jar包中没有那个类,而最新下载的ant.jar包中有那个类,那么实际上在Windows-->preferences-->Ant-->runtime-->classpath-->Ant home Entires-->add External jar时直接把最新的ant.jar包加进来也是可以解决问题的。

原创粉丝点击