1

I need to run the below comand using java but it is running fine in terminal as

svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k > output.txt

But when running the same via process builder it is returning empty result.

My code:

    ProcessBuilder pb = new ProcessBuilder("cmd", "C:\\Users\\dev112\\output", "svn", "list", "http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs", "--username", "prasadh", "--password", "prasadh2k", ">", "output.txt");
    pb.redirectErrorStream(true);
    try {
        Process p = pb.start();
        new Thread(new InputConsumerforImageMagick.InputConsumer(p.getInputStream())).start();
        try {
            System.err.println("Exited with: " + p.getErrorStream());
        } catch (Exception ex) {
            Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (IOException ex) {
        Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
    }
Prasath Bala
  • 698
  • 1
  • 7
  • 12

3 Answers3

0

Don't go through cmd. Just run the command directly:

final Path cwd = Paths.get("c:\\Users\\dev112\\output");
Files.createDirectories(cwd);
final Path outfile = cwd.resolve("output.txt");

final ProcessBuilder pb = new ProcessBuilder("svn", "list",
    "http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs",
    "--username", "prasadh", "--password", "prasadh2k");

pb.directory(cwd.toFile());
pb.redirectOutput(outfile.toFile());

final int retcode = pb.start().waitFor();

What is more, why do you get the process' standard output if you output to a file? Do one or the other, not both. If you output to a file then read the contents of that file after the command is executed.

The sample above outputs to a file; just open a stream to that file afterwards using Files.newInputStream(outfile) (well, that is, if retcode is 0; if it isn't, your command has ended with an error; which also means that you should redirect stderr somewhere, too)

fge
  • 119,121
  • 33
  • 254
  • 329
0

I/O redirection doesn't work well with ProcessBuilder. You should either call cmd.exe with

new ProcessBuilder("cmd", "/c", "svn ... > output.txt");

(i.e. you have to call cmd with exactly two arguments) or you must redirect yourself, that is you need to start a background thread which reads stdout from the process and writes it to output.txt. In that case, you should use:

new ProcessBuilder("svn", "list", ...);

The former is brittle when you have spaces in arguments. So I suggest the latter even though the Java code is much more complex.

You should also have a look at Commons Exec which makes it much easier to deal with external processes.

Or with Java 7, you can use pb.redirectOutput();

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • "you need to start a background thread which reads stdout from the process and writes it to output.txt" <-- eh? `ProcessBuilder` has `.redirectOutput()`... – fge Jan 23 '15 at 08:20
-1

This works for me:

String command = "svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k";
ProcessBuilder processBuilder = new ProcessBuilder(command.split());
processBuilder.redirectOutput(new File("C:/Users/dev112/output/", "output.txt"));
processBuilder.start();
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • 1
    Error: java.io.IOException: Cannot run program "": CreateProcess error=87, The parameter is incorrect at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) at org.archiveindexer.dao.RuntimeDemo.main(RuntimeDemo.java:33) Caused by: java.io.IOException: CreateProcess error=87, The parameter is incorrect at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:385) at java.lang.ProcessImpl.start(ProcessImpl.java:136) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 1 more – Prasath Bala Jan 23 '15 at 09:20