我看着this question,但它並沒有真正解決我的問題,所以我想我會發佈一個新的。如何使用Ant創建一個捆綁的可運行jar包
我需要使用Ant創建一個可運行jar(只需通過雙擊可運行)。我有下面的java代碼和build.xml文件,編譯代碼就好了,並創建了一個jar文件,但是當我嘗試通過雙擊運行jar時,我收到一條消息:「找不到主類:HttpController。 java的「。
我懷疑我的問題與加載外部Apache Http.jar
有關,因爲我已經成功構建並運行一個相同項目的jar,只是它沒有引用任何外部jar。
這裏是我的代碼:
HttpController.java:
package pack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpMessage;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpController {
public static void main(String[] args) {
DefaultHttpClient client = new DefaultHttpClient();
HttpHost httphost = new HttpHost("localhost", 80);
try {
HttpMessage req = new HttpGet("/test.html");
HttpResponse resp = client.execute(httphost, (HttpGet) req);
HttpEntity entity = resp.getEntity();
BufferedReader in = new BufferedReader(new InputStreamReader(
entity.getContent()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// shutdown the connection
client.getConnectionManager().shutdown();
}
}
}
的build.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Test" basedir="." default="jar">
<property name="source.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="class.dir" value="bin"/>
<property name="jar.dir" value="dist"/>
<property name="main-class" value="pack.HttpController"/>
<path id="libraries.path">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="delete old files">
<delete dir="${class.dir}"/>
<delete dir="${jar.dir}"/>
</target>
<target name="compile" description="build class files" depends="clean">
<mkdir dir="${class.dir}"/>
<javac srcdir="${source.dir}" destdir="${class.dir}">
<classpath refid="libraries.path"/>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<mkdir dir="${jar.dir}/${lib.dir}"/>
<copy todir="${jar.dir}/${lib.dir}" flatten="true">
<path refid="libraries.path"/>
</copy>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${jar.dir}/${lib.dir}/Apache HTTP.jar"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
</project>
MANIFEST.MF:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_31-b05 (Sun Microsystems Inc.)
Main-Class: HttpController
Class-Path: dist/lib
編輯 build.xml已根據Mike的回答進行了更新。問題仍然沒有解決。根據Danation的回答,還張貼了清單文件的內容。
@ see http://one-jar.sourceforge.net/index.php?page=build-tools&file=ant-example。 – Jayan 2012-03-26 16:49:42
@jayan是否可以在沒有任何外部工具的情況下執行此操作?即只使用Ant並編寫我自己的build.xml文件? – ewok 2012-03-26 16:52:42
有關於使用eclipse創建可運行jar的問題/答案等。請參閱http://stackoverflow.com/questions/502960/eclipse-how-to-build-an-executable-jar-with-external-jar。我更喜歡一個jar文件,因爲它創建了一個可運行的文件。加入螞蟻應該花10分鐘。那麼爲什麼不試試看結果呢? – Jayan 2012-03-26 17:00:06