我查看了这个问题,但它并没有真正解决我的问题,所以我想发布一个新的问题。
我需要使用Ant创建可运行的JAR文件(通过双击运行)。我有以下Java代码和build.xml文件,可以很好地编译代码并创建一个jar文件,但是当我尝试通过双击运行该jar时,会出现一条消息,显示“找不到主类:HttpController.java”。
我怀疑我的问题与加载外部的
以下是我的代码:
HttpController.java:
我需要使用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
编辑 根据Mike的回答,build.xml已更新。问题仍未解决。还按照Danation的回答发布了清单文件的内容。
Class-Path
接受文件名列表,如所述文档。不接受目录。 - user330315ant
,然后双击生成的jar文件来执行代码。此外,我希望除了Ant本身之外不必安装其他外部工具。我需要知道如何仅使用Ant来完成这个任务。 - ewok