title: java.lang.ClassNotFoundException when running java -jar
body: <p>I'm using ant to build my <code>build.xml</code> file, it compiles ok, but then getting a runtime <code>java.lang.NoClassDefFoundError</code> when running the resulting jar via "<code>java -jar my_jar.jar</code>". It seems like this comes up a lot but none of the related questions' solutions worked for me.</p>
  
  <p>My classpath for <code>javac</code> contains only "<code>/usr/local/lib/libthrift.jar</code>" and the main <code>.java</code> file imports a bunch of thrift packages such as <code>org.apache.thrift.transport.TTransportException</code>.</p>
  
  <p>When I try running the program via:</p>
  
  <pre><code>java -jar MyClass.jar
  </code></pre>
  
  <p>, I get the error:</p>
  
  <pre><code>Exception in thread "main" **java.lang.NoClassDefFoundError**: org/apache/thrift/transport/TTransportException
  Caused by: java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransportException
          at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
          at java.security.AccessController.doPrivileged(Native Method)
          at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
          at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
          at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
  Could not find the main class: **MyClass**. Program will exit.
  </code></pre>
  
  <p>Here are the things I've tried so far that don't work:</p>
  
  <ul>
  <li><p>adding a flag on the command line like "java <strong><code>-cp /usr/local/lib/libthrift.jar</code></strong> <code>-jar my_jar.jar</code>", the result is the same error as above</p></li>
  <li><p>adding <strong><code>&amp;lt;attribute name="Class-Path" value="./:/usr/local/lib/libthrift.jar"/&gt;</code></strong> inside my jar's <code>manifest&gt;</code> tag, the result is the same error as above</p></li>
  <li><p>adding <strong><code>-Xbootclasspath/a:/usr/local/lib/libthrift.jar:./</code></strong> to the java command line. it solves the first error but a different error comes up:</p>
  
  <p>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
          at org.apache.thrift.transport.TServerSocket.&lt;clinit&gt;(TServerSocket.java:36)
          at MyClass.start(Unknown Source)
          at MyClass.main(Unknown Source)</p></li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>If I comment out the code that instantiates the missing classes but leave the imports, the code executes fine.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I moved my java classes to a server and referenced the MainClass with the server in the manifest attribute, but that didn't fix anything.</p>