title: Using ANT in Java to extract files
body: <p>I'm attempting to use the functionality of Ant to extract the contents of compressed files to a temporary folder. Obviously I'm doing one or more things wrong here, but what they happen to be is escaping me as examples of this type of code on the web seem to be few and outdated:</p>
  
  <pre><code>//...
  
  public static void extract(File src, File dest){
      String path = src.getPath();
      String ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
  
      Expander expander = null;
  
      if(ext.equals("tar")) {
          expander = new Expander("untar","untar");
      } else if(ext.equals("jar")) {
          expander = new Expander("unzip","unzip");
      } else if(ext.equals("zip")) {
          expander = new Expander("unzip","unzip");
      } else {
          // ...
      }
  
      expander.setSrc(src);
      expander.setDest(dest);
      expander.execute();
  }
  //...
  </code></pre>
  
  <p>The above is based on: <a href="http://www.jajakarta.org/ant/ant-1.6.1/docs/en/manual/antexternal.html" rel="nofollow">http://www.jajakarta.org/ant/ant-1.6.1/docs/en/manual/antexternal.html</a></p>
  
  <p>The Expander class:</p>
  
  <pre><code>import org.apache.tools.ant.Project;
  import org.apache.tools.ant.Target;
  import org.apache.tools.ant.taskdefs.Expand;
  
  class Expander extends Expand{
      public Expander(String taskType, String taskName){
          this.setProject(new Project());
          this.getProject().init();
          this.setOwningTarget(new Target());
          this.setTaskType(taskType);
          this.setTaskName(taskName);
      }
  }
  </code></pre>
  
  <p>Inside the Expander constructor I get the following at run-time:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClassCond(Unknown Source)
      at java.lang.ClassLoader.defineClass(Unknown Source)
      at java.security.SecureClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.access$000(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at org.apache.tools.ant.Project.initProperties(Project.java:313)
      at org.apache.tools.ant.Project.init(Project.java:300)
      at Expander.&lt;init&gt;(Expander.java:8)
      at Main.extract(Main.java:151)
      at Main.getDeliverableFiles(Main.java:130)
      at Main.main(Main.java:36)
  caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.AntMain
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      ... 18 more
  </code></pre>