title: NoClassDefFoundError from inside a jar
body: <h1>Context</h1>
  
  <p>I made a small proof of concept project in order to learn to use ant:</p>
  
  <p>I created a small JAR file that only contains one class that you can see here:</p>
  
  <pre><code>public class Dummy {
  
      private String name;
  
      public Dummy(String name) {
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  }
  </code></pre>
  
  <p>I made a JAR of that called <code>lib/dummy.jar</code>. Here is its manifest:</p>
  
  <pre><code>Manifest-Version: 1.0
  Ant-Version: Apache Ant 1.10.5
  Created-By: 1.8.0_232-b09 (Oracle Corporation)
  Class-Path: 
  
  </code></pre>
  
  <h1>Project</h1>
  
  <p>Now, I want to use that JAR in a main class that looks like this:</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          Dummy f = new Dummy("Hello, World!");
          System.out.println(f.getName());
      }
  }
  </code></pre>
  
  <p>I can compile it without a problem (if I include my JAR in the classpath).</p>
  
  <p>Then, I try to make a JAR of that main class. The JAR contains this:</p>
  
  <pre><code>META-INF/
  |- MANIFEST.MF
  Main.class
  dummy.jar
  </code></pre>
  
  <p>The manifest contains:</p>
  
  <pre><code>Manifest-Version: 1.0
  Ant-Version: Apache Ant 1.10.5
  Created-By: 1.8.0_232-b09 (Oracle Corporation)
  Main-Class: Main
  Class-Path: dummy.jar
  </code></pre>
  
  <p>The JAR is created successfully.</p>
  
  <h1>Problem</h1>
  
  <p>If I try to run the JAR, I've got an <code>Exception in thread "main" java.lang.NoClassDefFoundError: Dummy</code></p>
  
  <p>The code should work, because if I include my initial <code>dummy.jar</code> in the classpath when running, everything works fine. (<code>java -cp build/jar/run-me.jar:lib/dummy.jar Main</code>)</p>
  
  <p>Why is it not working when I run the JAR all by itself?</p>
  
  <h1>build.xml</h1>
  
  <p>If it is of any use, here is my <code>build.xml</code> (improvements gladly accepted in the comments):</p>
  
  <pre><code>&lt;project default="all"&gt;
      &lt;path id="build.classpath"&gt;
          &lt;fileset dir="lib"&gt;
              &lt;include name="**/*.jar" /&gt;
          &lt;/fileset&gt;
      &lt;/path&gt;
      &lt;pathconvert property="mf.classpath" pathsep=" "&gt;
          &lt;path refid="build.classpath" /&gt;
          &lt;flattenmapper /&gt;
      &lt;/pathconvert&gt;
  
      &lt;target name="all" depends="compile,jar,run" /&gt;
  
      &lt;target name="compile"&gt;
          &lt;mkdir dir="build/classes" /&gt;
          &lt;javac srcdir="src" destdir="build/classes" includeantruntime="false"&gt;
              &lt;classpath&gt;
                  &lt;fileset dir="lib"&gt;
                      &lt;include name="**/*.jar" /&gt;
                  &lt;/fileset&gt;
              &lt;/classpath&gt;
          &lt;/javac&gt;
      &lt;/target&gt;
  
      &lt;target name="jar"&gt;
          &lt;mkdir dir="build/jar" /&gt;
          &lt;jar destfile="build/jar/run-me.jar" basedir="build/classes"&gt;
              &lt;manifest&gt;
                  &lt;attribute name="Main-Class" value="Main" /&gt;
                  &lt;attribute name="Class-Path" value="${mf.classpath}" /&gt;
              &lt;/manifest&gt;
              &lt;path id="build.classpath"&gt;
                  &lt;fileset dir="lib"&gt;
                      &lt;include name="**/*.jar" /&gt;
                      &lt;include name="**/*.zip" /&gt;
                  &lt;/fileset&gt;
              &lt;/path&gt;
          &lt;/jar&gt;
      &lt;/target&gt;
  
      &lt;target name="run"&gt;
          &lt;java jar="build/jar/run-me.jar" fork="true"&gt;&lt;/java&gt;
      &lt;/target&gt;
  
      &lt;target name="clean"&gt;
          &lt;delete dir="build" /&gt;
      &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>