title: Run ant from Java
body: <p>Is there a tutorial on how to run Ant from Java? I got some code from here: <a href="https://stackoverflow.com/questions/652053/setting-java-home-when-running-ant-from-java">Setting JAVA_HOME when running Ant from Java</a></p>
  
  <p>But haven't been able to make it work. I've been trying to find an example or tutorial on how to actually use it.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre>
          Project p = new Project();
          p.setUserProperty("ant.file", buildFile.getAbsolutePath());
          p.fireBuildStarted();
          p.init();
          p.executeTarget("default");
  </pre>
  
  <p>But I guess this error:</p>
  
  <pre>
  Exception in thread "main" Target "default" does not exist in the project "null". 
      at org.apache.tools.ant.Project.tsort(Project.java:1912)
      at org.apache.tools.ant.Project.topoSort(Project.java:1820)
      at org.apache.tools.ant.Project.topoSort(Project.java:1783)
      at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
      at com.arthrocare.vss2svn.VSS2SVN.newProcess(VSS2SVN.java:128)
      at com.arthrocare.vss2svn.VSS2SVN.main(VSS2SVN.java:52)
  Java Result: 1
  </pre>
  
  <p>I tried specifying the project with:</p>
  
  <pre>
  p.setUserProperty("ant.project.name", "VSS Project");
  </pre>
  
  <p>But no luck.</p>
  
  <p>The ant file specified is correct as it works perfectly from the command line.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After some more searching I got here: <a href="http://onjava.com/pub/a/onjava/2002/07/24/antauto.html?page=1" rel="noreferrer">http://onjava.com/pub/a/onjava/2002/07/24/antauto.html?page=1</a></p>
  
  <p>It is a great tutorial.</p>
  
  <p>Here's the code I got a little bit earlier than seeing the code in the answer below:</p>
  
  <pre>
          Project project = new Project();
          ProjectHelper.configureProject(project, buildFile);
          DefaultLogger consoleLogger = new DefaultLogger();
          consoleLogger.setErrorPrintStream(System.err);
          consoleLogger.setOutputPrintStream(System.out);
          consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
          project.addBuildListener(consoleLogger);
          project.init();
          project.executeTarget(project.getDefaultTarget());
  </pre>
  
  <p>But for some reason the task still fails! I'm using a Visual Source Safe task that needs to read an environment value at runtime but it doesn't see it with this approach. Running the build.xml file manually and with the following code works:</p>
  
  <pre>
          ProcessBuilder pb = new ProcessBuilder();
          Map env = pb.environment();
          String path = env.get("ANT_HOME");
          System.out.println(path);
          pb.directory(new File(System.getProperty("user.home")));
          pb.command(path + System.getProperty("file.separator")
                  + "bin" + System.getProperty("file.separator") + "ant.bat");
          try {
              Process p = pb.start();
          } catch (IOException ex) {
              //
          }
  </pre>