I'm trying to get my java program to run an svn command from the command prompt, which will write logs to an xml file.
This is what I want it to do:
Runtime.getRuntime().exec("cmd.exe /c svn log /location/ --xml > c:\\output.xml");
however, it will not print anything to the xml file.
when I enter the "svn log /location/ --xml > output.xml" directly into cmd, though, it will print the logs as expected, into the xml file.
furthermore, when I use the following code, it will print "test" into the xml file without problems.
Runtime.getRuntime().exec("cmd.exe /c echo \"test\" > c:\\work\\output.xml"开发者_如何学Python);
OK, after reading When Runtime.exec() Wont, I've determined that for some reason, svn is not recognized when I run the command with java, but it is perfectly fine when I enter it manually into the command line
Any ideas? Let me know if you have any questions that i might be able to help you with.
AFAIU (from what I've seen in similar questions on forums) redirection (>
) does not work when used in Runtime.exec()
.
Follow advise from here http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html
Consider the following line of code:
Process p = Runtime.getRuntime().exec("/bin/sh -c > /bin/ls > ls.out");
This is intended to execute a Bourne shell and have the shell execute the ls command, redirecting the output of ls to the file ls.out. The reason for using /bin/sh is to get around the problem of having stdout redirected by the Java internals. Unfortunately, if you try this nothing will happen. When this command string is passed to the exec() method it will be broken into an array of Strings with the elements being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail, as sh expects only a single argument to the "-c" switch. To make this work try:
String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
Process p = Runtime.getRuntime().exec(cmd);
Since the command line is already a series of Strings, the strings will simply be loaded into the command array by the exec() method and passed to the new process as is. Thus the shell will see a "-c" and the command "/bin/ls > ls.out" and execute correctly.
I suggest that you change your command to
String[] cmd = {
"cmd.exe",
"/c",
"c:\\path\\to\\svn log /location/ --xml > c:\\output.xml"
};
Process p = Runtime.getRuntime().exec(cmd);
Can you try giving full path of your svn binary in the first exec
method call.
精彩评论