Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:05:08+00:00 2026-05-23T21:05:08+00:00

Is there a tutorial on how to run Ant from Java? I got some

  • 0

Is there a tutorial on how to run Ant from Java? I got some code from here: Setting JAVA_HOME when running Ant from Java

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.

Here’s what I have so far:

        Project p = new Project();
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());
        p.fireBuildStarted();
        p.init();
        p.executeTarget("default");

But I guess this error:

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

I tried specifying the project with:

p.setUserProperty("ant.project.name", "VSS Project");

But no luck.

The ant file specified is correct as it works perfectly from the command line.

UPDATE

After some more searching I got here: http://onjava.com/pub/a/onjava/2002/07/24/antauto.html?page=1

It is a great tutorial.

Here’s the code I got a little bit earlier than seeing the code in the answer below:

        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());

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:

        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) {
            //
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:05:09+00:00Added an answer on May 23, 2026 at 9:05 pm

    Is there a tutorial on how to run Ant from Java?

    Part of my answer to this question might help:

    See this article
    and this article:

       File buildFile = new File("build.xml");
       Project p = new Project();
       p.setUserProperty("ant.file", buildFile.getAbsolutePath());
       p.init();
       ProjectHelper helper = ProjectHelper.getProjectHelper();
       p.addReference("ant.projectHelper", helper);
       helper.parse(p, buildFile);
       p.executeTarget(p.getDefaultTarget());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run Eclipse Java Development Tools form source code, but I'm stuck
Is there a good tutorial or does anyone have experience with setting this up
I'm using the Hello World with Ant tutorial from the Ant manual to learn
I'm following the rails tutorial here: http://railstutorial.org/chapters/filling-in-the-layout#top When I run rspec spec/, I get
I'm in the process of porting some code from Linux to Mac OS X.
Is there a way to run the following script via PHP http://www.magentoadvisor.com/magento-backup/tutorial-magento-backup-scripts-part-2/ I have
I tried to run example here http://download.oracle.com/javaee/6/tutorial/doc/gipzz.html and didnt work... There is a bug
I'm trying to follow the tutorial here to setup a headless selenium test-run with
I am following Java Hibernate tutorial example from YouTube. Everything looks great until I
Is there a good tutorial on using regular expressions, especially with grep ? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.