---
title: "ant学习笔记（六）"
author: "zzkathy"
date: "2008-01-09T15:32:00Z"
tags: []
slug: "ant"
draft: false
lastmod: "2008-01-09T15:32:00Z"
id: 158213
url: "https://zzkathy.iteye.com/blog/158213"
---

今天写了一段ant脚本，是用来编译，打包，运行java的。具体如下：

 &lt;target name="compile"&gt;  
  &lt;javac srcdir="${src}"  
             destdir="${bin}"  
             source="1.4"  
       encoding="UTF-8"  
       deprecation="on"  
       failonerror="true"  
    &gt;  
   &lt;classpath&gt;  
       &lt;fileset dir="${lib}"&gt;  
         &lt;include name="\*.jar"/&gt;  
       &lt;/fileset&gt;  
   &lt;/classpath&gt;         
  &lt;/javac&gt;  
 &lt;/target&gt;  
   
 &lt;target name="makejar" depends="compile"&gt;  
  &lt;jar destfile="${jar}/${jar.name}" basedir="${bin}"&gt;  
   &lt;manifest&gt;  
    &lt;attribute name="Main-Class" value="${main.class}"/&gt;  
   &lt;/manifest&gt;  
  &lt;/jar&gt;  
 &lt;/target&gt;  
   
 &lt;target name="run" depends="makedbjar"&gt;  
  &lt;java classname="${main.class}"  
   fork="true"  
   failonerror="yes"  
   &gt;  
   &lt;classpath&gt;  
    &lt;pathelement location="${jar}/${jar.name}"/&gt;  
    &lt;fileset dir="${lib}"&gt;  
      &lt;include name="\*.jar"/&gt;  
    &lt;/fileset&gt;  
   &lt;/classpath&gt;  
  &lt;/java&gt;  
 &lt;/target&gt;

大家注意其中的红色部分，喵喵一开始写的时候是jar="${jar}/${jar.name}" ，然后就一直出错。查阅了ant文档之后才发现其中有一句：When using the `jar` attribute, all classpath settings are ignored according to [Sun's specification](http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#-jar). 顿时汗如雨下，作了如上更改后终于得以成功-\_-b