0

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

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Aelfhere
  • 171
  • 2
  • 12
  • Are you capturing any output or errors and printing them to your logs? i.e. is `svn` failing? – Peter Lawrey Jun 03 '11 at 17:30
  • 4
    Please read [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). All 4 pages. Then please edit your question to include detail about the *real* problem. – BalusC Jun 03 '11 at 17:34
  • ok, @BalusC, I read the 4 pages... and I understand what my problem above is, however I still can't get it to work. In fact, I can't even get the correct solution on page 4 to work. Perhaps I'm a complete idiot, but I need this explained to me as though I'm 2 years old, with very specific instructions on what to do. I don't know how to describe the REAL problem, because frankly, I don't know what the real problem is. – Aelfhere Jun 03 '11 at 19:51
  • The article should have given you insights how to capture the actual command console output (and errors, if any), like as if you're entering the command yourself in the console. *This* output should contain the answer to your problem. For example, `svn: Unknown command` or something. This information should help you in nailing down the problem better. – BalusC Jun 03 '11 at 20:14
  • ok, the question is updated with important information now – Aelfhere Jun 03 '11 at 20:47

3 Answers3

3

AFAIU (from what I've seen in similar questions on forums) redirection (>) does not work when used in Runtime.exec().

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Indeed, redirection is a feature of the shell; see also this [thread](http://stackoverflow.com/questions/5740390). – trashgod Jun 04 '11 at 09:14
2

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);
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
1

Can you try giving full path of your svn binary in the first exec method call.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • well, I can't give you a full path, since it is a company's database... but I can tell you that it is https: //IP_address/svn/etc/ – Aelfhere Jun 03 '11 at 17:37
  • No I didn't mean path of your svn repository. What I meant was to provide full path to you svn client (`svn.exe`) inyour exec call. – anubhava Jun 03 '11 at 17:53
  • Oh... yeah, that was the problem. For some reason, putting in "c:\program files\...\svn" worked, whereas just having svn didn't work. Thanks very much – Aelfhere Jun 06 '11 at 15:34