title: how to create a bundled runnable jar using Ant
body: <p>I looked at <a href="https://stackoverflow.com/questions/9806566/run-a-jar-file-generated-by-ant-in-command-line">this question</a>, but it didn't really solve my problem, so I figured I'd post a new one.</p>
  
  <p>I need to create a runnable jar (runnable simply by double clicking) using Ant.  I have the following java code and build.xml file, which compiles the code just fine and creates a jar file, but when I try to run the jar by double clicking, i get a message saying "Could not find main class: HttpController.java."</p>
  
  <p>I have the suspicion that my problem has to do with loading the external <code>Apache Http.jar</code>, as I have successfully built and run a jar for a project that is identical, except that it does not reference any external jars.</p>
  
  <p>Here is my code:</p>
  
  <p>HttpController.java:</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>
  
  <p>build.xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
      &lt;project name="Test" basedir="." default="jar"&gt;
      &lt;property name="source.dir"     value="src"/&gt;
      &lt;property name="lib.dir"        value="lib"/&gt;
      &lt;property name="class.dir"      value="bin"/&gt;
      &lt;property name="jar.dir"        value="dist"/&gt;
      &lt;property name="main-class"     value="pack.HttpController"/&gt;
  
      &lt;path id="libraries.path"&gt;    
          &lt;fileset dir="${lib.dir}"&gt;
              &lt;include name="*.jar"/&gt;
          &lt;/fileset&gt;
      &lt;/path&gt;
  
      &lt;target name="clean" description="delete old files"&gt;
          &lt;delete dir="${class.dir}"/&gt;
          &lt;delete dir="${jar.dir}"/&gt;
      &lt;/target&gt;
  
      &lt;target name="compile" description="build class files" depends="clean"&gt;
          &lt;mkdir dir="${class.dir}"/&gt;
          &lt;javac srcdir="${source.dir}" destdir="${class.dir}"&gt;
              &lt;classpath refid="libraries.path"/&gt;
          &lt;/javac&gt;
      &lt;/target&gt;
  
      &lt;target name="jar" depends="compile"&gt;
          &lt;mkdir dir="${jar.dir}"/&gt;
          &lt;mkdir dir="${jar.dir}/${lib.dir}"/&gt;
          &lt;copy todir="${jar.dir}/${lib.dir}" flatten="true"&gt;
              &lt;path refid="libraries.path"/&gt;
          &lt;/copy&gt;
          &lt;jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}"&gt;
              &lt;manifest&gt;
                  &lt;attribute name="Main-Class" value="${main-class}"/&gt;
                  &lt;attribute name="Class-Path" value="${jar.dir}/${lib.dir}/Apache HTTP.jar"/&gt;
              &lt;/manifest&gt;
          &lt;/jar&gt;  
      &lt;/target&gt;
  
      &lt;target name="run" depends="jar"&gt;
          &lt;java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/&gt;
      &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>MANIFEST.MF:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>EDIT</strong> build.xml has been updated as per Mike's answer.  Problem is still not solved.  Also posted contents of manifest file, as per Danation's answer.</p>