0

I want to run an ant script runANTScript.xml from my java class. The script is located in SCRIPTS/ folder of my web application. I tried to run file like this

    StringBuffer output = new StringBuffer();

    String command="ant -f SCRIPTS/runANTScript.xml"
    Process p;
    try {

        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

But this gives an error

java.io.IOException: Cannot run program "ant": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at my.Abc.executeCommand(Abc.java:48)

I am able to run by using code below but it import classes from ant.jar and i don't want to use jar file.

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
 Project project = new Project();
    File buildFile = new File(buildXmlFileFullPath);
    project.setUserProperty("ant.file", buildFile.getAbsolutePath());
    project.addBuildListener(consoleLogger);

    // Capture event for Ant script build start / stop / failure
    try {
        project.fireBuildStarted();
        project.init();
        ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
        project.addReference("ant.projectHelper", projectHelper);
        projectHelper.parse(project, buildFile);

        // If no target specified then default target will be executed.
        String targetToExecute = (target != null && target.trim().length() > 0) ? target.trim() : project.getDefaultTarget();
        project.executeTarget(targetToExecute);
        project.fireBuildFinished(null);
        success = true;
    }

3 Answers 3

1

If you don't mind adding groovy to your classpath you could use AntBuilder

Sign up to request clarification or add additional context in comments.

Comments

1

You need

  1. to include the path of ant executable so that the java exec() knows where to find it.

or

  1. Call ant with its absolute path.

The first task is a bit complicated, as PATH environmental variable is evaluated really early during the boot process (e.g. java -Djava.library.path=/path/to/ant)

Changing the system property later doesn’t have any effect. You could still use (or abuse?) the classloader:

System.setProperty( "java.library.path", "/path/to/ant" );

Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );

1 Comment

Add ant libraries to your classapth and replace call Runtmie.exec(...) with
1

Different Approach

Add Ant libraries to your classapth and replace call Runtmie.exec(...) with org.apachetools.ant.Main.main(...)

This is more stable, and has no dependency on a local Ant installation. Which need to be on the system environment PATH, or called with the full path in the Runtime.exec(...)

This will make Ant part of the created applciation and not a externally called application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.