1

I'm trying to launch an external program from my java swing app using this:

Process proc = Runtime.getRuntime().exec(cmd);

But the external program never actually gets launched until I close out of my java app...everytime. It waits to launch only after I have closed out.

the external program I am trying to run is an exe that takes arguments so:

cmd = "externalProgram.exe -v --fullscreen --nowing";

What could possibly be wrong here. Funny enough it works as expected if i try something simple like:

Process proc = Runtime.getRuntime().exec("notepad.exe");
Glstunna
  • 1,993
  • 3
  • 18
  • 27

2 Answers2

7

You may need to read from the process's standard output, or close the standard input, before it will proceed. For reading the output, the problem is that the buffer can get full, blocking the program; for closing the input, the problem is that some programs will try to read data from there if it's available, waiting to do so. One or both of these tricks is very likely to straighten things out for you.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    Thanks man. I closed ALL the blasted streams and it seems to work now. Process p = Runtime.getRuntime().exec(cmd.toString()); p.getInputStream().close(); p.getOutputStream().close(); p.getErrorStream().close(); Just closing the input stream didn't work for some reason. – Glstunna Apr 10 '11 at 03:48
2

You may also read the error output stream to check it the program is actually being unsuccessfully executed

String cmd = "svn.exe";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line=reader.readLine())!=null){
   System.out.println(line);
}
reader.close();

My console shows

Type 'svn help' for usage.

Which evidently shows the program was executed by Java.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • Thanks. I didn't need any of the error outputs in my java app as it's meant to be a silent launch so I just closed the streams and it works. – Glstunna Apr 10 '11 at 03:51