0

I am using this code:

ProcessBuilder builder = new ProcessBuilder("cmd.exe","java","invalidArg");
builder.redirectErrorStream(true);
try {
 Process p = builder.start();
 BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()),10240);
 String line;
 if(processIsTerminated(p)){
 line = r.readLine();
 }
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}

Is there a way to have the cmd window open as well when I use the .start() function? Currently it runs hidden and if there is no response I don't know if my command was ran successfully or it didn't run at all.

asked Oct 3, 2014 at 7:34
1
  • I don't think it's possible, because ProcessBuilder only takes the inputs and passes to the process you pass in the first argument. Commented Oct 3, 2014 at 7:39

1 Answer 1

1

Well you have only 2 ways of running a command under Windows :

  • the way you go : you have full access to the input, output and error stream of the lauched command, but it has no window and anyway it would not write anything there since your program gets all the output
  • ask cmd.exe to start the other command in its own window (and optionnaly wait for its end). But then as the command writes to its own window, you program has no access to the input, output or error streams of the command

You can obtain that 2nd execution way with the start [/w] command arguments command of cmd.exe, just type start /w cmd.exe /c "echo foo & pause" in a cmd window to see what happens (the & pause is only there to let you some time to read the output ...)

From java, it would be :

ProcessBuilder builder = new ProcessBuilder("cmd.exe","/c", "start", "/w",
 "cmd", "/c", "java invalidArg & pause");
answered Oct 3, 2014 at 9:32
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used this method to debug my code and then went back to the prior method in order to have access to the input/output. Ideally I would like to have the window open AND have access to the input/output streams...

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.