title: Including libraries in jar
body: <p>Ok, so here's what we've got:</p>
  
  <p>We've got two library packages, which we've compiled into jars.</p>
  
  <pre>
  package starwars; 
  public class JarJar {
  
      public void JarSayHello()
      {
          System.out.println("Jaaaaar!!"); 
      }
  
  }
  
  </pre>
  
  <pre>
  package barwars; 
  public class BarBar {
  
      public void BarSayHello()
      {
          System.out.println("Baaaaa!!"); 
      }
  
  }
  
  </pre>
  
  <p>We compile these with</p>
  
  <pre>
  javac -d bin -sourcepath src src/barwars/BarBar.java
  jar cvf barwars.jar -C bin . 
  </pre>
  
  <p>and </p>
  
  <pre>
  javac -d bin -sourcepath src src/starwars/JarJar.java
  jar cvf starwars.jar -C bin . 
  </pre>
  
  <p>All nicely into jars for us.</p>
  
  <p>Now we want to include these two jars into another java project.</p>
  
  <p>so we've got</p>
  
  <ul>
  <li>/project/src/a_pack/HelloWorld.java </li>
  <li>/project/libs/starwars.jar   </li>
  <li>/project/libs/barwars.jar  </li>
  <li>/project/manifest.txt</li>
  </ul>
  
  <pre>
  package a_pack;
  import starwars.JarJar; 
  import barwars.BarBar; 
  
  public class HelloWorld {
  
      public static void main(String[] args) {    
          System.out.println("Hello, World");         
          JarJar myJar = new JarJar();
          myJar.JarSayHello();        
          BarBar myBar = new BarBar(); 
          myBar.BarSayHello(); 
      }
  
  }
  </pre>
  
  <p>Manifest.txt</p>
  
  <pre>
  Main-Class: a_pack.HelloWorld
  Class-Path: libs/starwars.jar libs/barwars.jar
  </pre>
  
  <p>Now we compile this with:</p>
  
  <pre>
  javac -d bin -sourcepath src -cp "libs/starwars.jar;libs/*" src/a_pack/HelloWorld.java 
  jar cvfm helloworld.jar manifest.txt -C bin . 
  </pre>
  
  <p>And this compiles and runs fine. </p>
  
  <p>Now I have two problems. </p>
  
  <p>Firstly - if I move this jar file to somewhere else, and try run it, then I'll get:</p>
  
  <p><code>Exception in thread "main" java.lang.NoClassDefFoundError: starwars/JarJar</code></p>
  
  <p>Now I can fix this by moving the libs folder to wherever I move the jar. But this strikes me as messy (what if there is already a libs folder in that location?).</p>
  
  <p>Ideally what I'd like to do, is include the referenced jars inside the jar, so there's one jar that contains everything that's required to run inside itself. </p>
  
  <p>Is this possible? (And is it good practise?)</p>