2012-03-26 65 views
7

我看着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的回答,还张贴了清单文件的内容。

+0

@ see http://one-jar.sourceforge.net/index.php?page=build-tools&file=ant-example。 – Jayan 2012-03-26 16:49:42

+0

@jayan是否可以在没有任何外部工具的情况下执行此操作?即只使用Ant并编写我自己的build.xml文件? – ewok 2012-03-26 16:52:42

+0

有关于使用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

回答

0

看看你的清单文件,这很可能是一个问题。我只在清单文件不正确时才看到该错误。

只是猜测,但我认为它试图运行一个“.java”文件,因为它是主要的类,这是不会工作。

请参阅oracle教程:http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

如果这样做完全没有帮助,请在原始问题中发布清单文件的内容以及.jar文件的目录结构。

+0

我知道清单文件存在问题,但我需要知道如何重新配置​​build.xml,以便ant能够正确生成它。 – ewok 2012-03-26 15:45:50

+1

@ewok:发布清单文件的内容以及原始问题中.jar文件的目录结构。一旦发现问题,您应该更好地了解build.xml中的问题。发布了 – Danation 2012-03-26 15:49:05

+0

。我也根据Mike的回答 – ewok 2012-03-26 16:03:51

16

略...

我已经返工build.xml文件正确包括jar文件,并在清单类路径中的库。我假设你的“阿帕奇http.jar”文件是Apache核心的包装,并包含在它的其他几个jar文件为Apache客户端等

的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="jar.file"  value="${jar.dir}/${ant.project.name}.jar"/> 
    <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="${class.dir}/${lib.dir}"/> 
     <copy todir="${class.dir}/${lib.dir}" flatten="true"> 
      <path refid="libraries.path"/> 
     </copy> 

     <manifestclasspath property="manifest.classpath" jarfile="${jar.file}"> 
      <classpath refid="libraries.path"/> 
     </manifestclasspath> 

     <jar destfile="${jar.file}" basedir="${class.dir}"> 
      <manifest> 
       <attribute name="Main-Class" value="${main-class}"/> 
       <attribute name="Class-Path" value="${manifest.classpath}"/> 
      </manifest> 
     </jar> 
    </target> 

    <target name="run" depends="jar"> 
     <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> 
    </target> 

</project> 
+0

更新了build.xml,我做了推荐的编辑。我不得不改变一些东西(没有'$ {dist}'属性,我认为你的意思是'$ {jar.dir}'),但是构建仍然返回相同的错误。 – ewok 2012-03-26 16:01:42

+0

还请包括jar文件的目录布局。我想知道rt是否让您的课程处于默认包中感到不安。 – Mike 2012-03-26 16:08:45

+0

我将'HttpController.java'移动到了'pack'软件包(src/pack),并调整了build.xml文件以反映这一点,但同样的错误发生。 – ewok 2012-03-26 16:16:29

4

Class-Path条目是错误的,你必须单独指定每个jar文件,你不能指定一个带有.jar文件的目录(只有带有.jar文件的目录)。类文件)

这在JAR File specification记录,并在Java tutorial

解释相当不错。如果你有两个库命名FirstLib.jarSeconLib.jar您的类路径条目应该是这样的:

Class-Path: lib/FirstLib.jar lib/SecondLib.jar 

编辑
在你的build.xml里面看起来像这样:

<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}"> 
    <manifest> 
     <attribute name="Main-Class" value="${main-class}"/> 
     <attribute name="Class-Path" value="${lib.dir}/FirstLib.jar ${lib.dir}/SecondLib.jar"/> 
    </manifest> 
</jar> 
+0

我编辑了我的build.xml文件来反映这一点,但我仍然不断收到无主类错误。无论什么原因,它不认可我定义的主类 – ewok 2012-03-26 18:19:49

+0

你的主类是否真的没有包?因为这就是'Main-Class:HttpController'定义的内容。 – 2012-03-26 18:59:38

+0

我会更新上面的代码。最初它没有包装,但我已经添加了一个包装并仍然遇到同样的问题 – ewok 2012-03-26 19:01:30

1

这更多的是一种通用的解决方案的制作可执行的JAR文件,但是这是我使用的代码:我用这个结构,使许多不同的可执行的JAR文件

<?xml version="1.0" encoding="UTF-8"?> 

<!-- 
    Any words or attribute values which are capitalized and separated by 
    underscores are places where you add the data. 

    Example: PROJECT_NAME, enter your project name 
--> 

<project name="PROJECT_NAME" default="jar" basedir="."> 
    <!-- source code package --> 
    <property name="src" value="SOURCE_CODE_FOLDER"/> 

    <!-- classes folder (will be deleted) --> 
    <property name="cls" value="classes"/> 

    <!-- the directory of the jar file --> 
    <property name="jar.dir" value="JAR_FILE_DIRECTORY"/> 

    <!-- the jar file itself --> 
    <property name="jar.file" value="${jar.dir}/${ant.project.name}-launch.jar"/> 

    <!-- the fully qualified name of the main class --> 
    <property name="main" value="PATH.TO.MAIN_CLASS"/> 

    <!-- initialization --> 
    <target name="-init"> 

     <!-- the class folder* --> 
     <mkdir dir="${cls}"/> 

     <!-- the jar directory --> 
     <mkdir dir="${jar.dir}"/> 
    </target> 

    <!-- compiling of files --> 
    <target name="-post-init-comp" depends="-init"> 

     <!-- 
      turn all of the source java classes into class files 
      located in the directory we made* 
     --> 
     <javac srcdir="${src}" destdir="${cls}"/> 
    </target> 

    <!-- creation of the jar file --> 
    <target name="jar" depends="-post-init-comp"> 

     <!-- make the executable jar --> 
     <jar destfile="${jar.file}" basedir="${cls}"> 
      <manifest> 

       <attribute name="Manifest-Version" value="1.0"/> 
       <attribute name="Ant-Version" value="Apache Ant 1.8.3"/> 

       <attribute name="Main-Class" value="${main}"/> 

       <!-- 
        for some reason, this is needed for me in order 
        to have the jar function right 
       --> 
       <attribute name="Class-Path" value="${main}"/> 

       <!-- Any other mainfest data is added here --> 

      </manifest> 
     </jar> 

     <!-- remove the class folder --> 
     <delete dir="${cls}"/> 
    </target> 
</project> 

,再加上它是简单比你所拥有的:-)

所有放在jar标签下的清单标签中的数据将成为一个自动生成的具有给定数据的清单文件。

相关问题