確認您已經安裝了ant.
如果沒有安裝,可以去這里下載
http://ant.apache.org/
安裝后,配置下path就可以了.
把ant目錄下的/bin加入系統環境變量中.
開始吧:
隨便找個目錄建個測試類:
我這里是在G:\ant目錄下.
先建個Test.java:
public class Test


{
public static void main(String args[])

{
System.out.println("First Ant Test!");
}
}
這個程序再簡單不過了!
接著在此目錄下創建build.xml(名字是固定的):
<?xml version="1.0" encoding="GBK" ?>
<!--default屬性,在運行ant命令時沒有指定target時,默認調用的target-->
<project default="main">
<!--depends依賴,按依賴順序執行-->
<target name="main" depends="compile,compress">
<!--控制臺輸出回顯信息-->
<echo>
Billding the .jar file!
</echo>
</target>
<!--編譯target-->
<target name="compile">
<echo>
compliing the .jar file!
</echo>
<javac srcdir="." />
</target>
<!--打包target-->
<target name="compress">
<echo>
Compressing the .jar file!
</echo>
<jar jarfile="Test.jar" basedir="." includes="*.class" />
</target>
</project>
好了準備工作都做好了.
打開命令行,進入當前目錄G:\ant
輸入:ant
回車看看輸出了什么?
Buildfile: build.xml

compile:
[echo]
[echo] compliing the .jar file!
[echo]

compress:
[echo]
[echo] Compressing the .jar file!
[echo]

main:
[echo]
[echo] Billding the .jar file!
[echo]

BUILD SUCCESSFUL
好了成功了,看看當前目錄下是不是多了Test.class和Test.jar文件.已經搞定了.
現在在命令行輸入:java -jar Test.jar
輸出:
Failed to load Main-Class manifest attribute from
Test.jar
有個錯誤,這已經和ant無關了,改一下jar包里的MANIFEST.MF文件就可以了.
用winrar打開,在最后一行加入
Main-Class: Test
好了搞定.
自己去體驗結果吧.