Reputation: 5156
I'm trying to execute a simple batch file from java using Runtime.getRuntime().exec(command); but facing issues, below is my code snippet
public class Path {
public static void main(String args[]){
String[] command = new String[3];
command[0]="cmd";
command[1]="/C";
command[2]="D:/alt/a.bat";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
and the batch a.bat has ant -f runme.xml test in it and runme.xml resides in D:/alt physical location which has a target test, so far so good but when i try to execute the java code above, below is the ouput
D:\RCPStack\Path>ant -f runme.xml test Buildfile: runme.xml does not
exist! Build failed
when i execute manually it works fine,seems to be the problem is with the code
below is manual execution output
how to tackle this(i don't know if the code is incorrect) and handle as a best practice
Upvotes: 3
Views: 1019
Reputation: 5156
p=Runtime.getRuntime().exec("cmd /c ant -f runme.xml test", null, new File("D:/alt"));//works
or
p=Runtime.getRuntime().exec("cmd /c a.bat", null, new File("D:/alt"));//works
Upvotes: 0
Reputation: 1401
Try to use the method Runtime.exec(String cmd, String[] envp, File dir)
to set the working directory to D:/alt/
.
This is because Ant must be executed in the directory where runme.xml is so Ant can find it.
Upvotes: 3