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);
 }
asked Jan 23, 2015 at 8:08
10
  • did you mean to drop the /c from "cmd /C command to run" I suggest you try running the whole command on the command line with the cmd Most likely you are getting an error and since you are ignoring the error stream you can't see it. Commented Jan 23, 2015 at 8:11
  • 1
    No am trying to write the output in a text file but it is creating text file but not writing any value ino it. Commented Jan 23, 2015 at 8:12
  • In that case you most likely need the /C Commented Jan 23, 2015 at 8:13
  • Why go through cmd at all? Commented Jan 23, 2015 at 8:15
  • 1
    When i try like this "ProcessBuilder pb = new ProcessBuilder("cmd", "/C:\\Users\\dev112\\output", "svn", "list", "192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/…", "--username", "prasadh", "--password", "prasadh2k", ">", "output.txt");" output file is not created Commented Jan 23, 2015 at 8:15

3 Answers 3

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

answered Jan 23, 2015 at 8:19
Sign up to request clarification or add additional context in comments.

1 Comment

"you need to start a background thread which reads stdout from the process and writes it to output.txt" <-- eh? ProcessBuilder has .redirectOutput()...
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)

answered Jan 23, 2015 at 8:17

7 Comments

Note: redirectOutput() is only available since Java 7.
@AaronDigulla yes, but then so is Path. And this is 2015, right? :p
final int retcode = pb.build().waitFor(); ---> am getting error in this line. I tried this too but same result
@PrasathBala yes, error from my part, I fixed it. It should have been .start(), not .build().
pb.redirectOutput(output.toFile()); ---> error near output.tofile
|
-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();
answered Jan 23, 2015 at 9:08

1 Comment

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.<init>(ProcessImpl.java:385) at java.lang.ProcessImpl.start(ProcessImpl.java:136) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 1 more

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.