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;
}
You need
exec()
knows where to find it. or
The first task is a bit complicated, as PATH environmental variable is evaluated really early during the boot process (eg 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 );
如果您不介意将Groovy添加到类路径中,则可以使用AntBuilder
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.