2

I've created a GUI (swing) that executes a batch file that contains a command prompt .exe file execution with specific parameters. When I run the batch file manually (by double clicking it), everything is as expected. The problem is: the command prompt window doesn't open to show progress, moreover, it doesn't really start to work (only initiated) until I exit the GUI (forking?). When it starts to work, is works somewhere in the background and seen only in the task manager. Only a blank command prompt window is opened.

From digging little bit around, I've constructed this command that gives me same result as above:

Runtime.getRuntime().exec("cmd.exe /c start \"Encoding\" cmd.exe /c start md \"" + Gui.outputDirField.getText() + "\\encoderOutput\" & cd \"" + Gui.outputDirField.getText() + "\\encoderOutput\" & \"" + Gui._batFile + "\" & pause");

Could you please assist?

Sorry if it sounds stupid..

Mark
  • 1,540
  • 2
  • 13
  • 21

3 Answers3

4

this way works for me:

new Thread() {
@Override public void run() {
try {    
Runtime.getRuntime().exec("cmd.exe /c start " + Gui._batFile);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}.run();
Mark
  • 1,540
  • 2
  • 13
  • 21
1

trashgod may be on to something. We ran into issues with paths with spaces. This is from the release notes for jre 7u21

Changes to Runtime.exec

On Windows platform, the decoding of command strings specified to Runtime.exec(String), Runtime.exec(String,String[]) and Runtime.exec(String,String[],File) methods, has been improved to follow the specification more closely. This may cause problems for applications that are using one or more of these methods with commands that contain spaces in the program name, or are invoking these methods with commands that are not quoted correctly.

For example, Runtime.getRuntime().exec("C:\\My Programs\\foo.exe bar") is an attempt to launch the program "C:\\My" with the arguments "Programs\\foo.exe" and "bar". This command is likely to fail with an exception to indicate "C:\My" cannot be found.

The example Runtime.getRuntime().exec("\"C:\\My Programs\\foo.exe\" bar") is an attempt to launch the program "\"C:\\My". This command will fail with an exception to indicate the program has an embedded quote.

Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.

Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
0

Does your bat file requiere user interaction or why are you putting a pause on your command? If so, the Runtime.exec just runs the file with no window, why would you want a Window? >ou can get a Process object as a result from the exec, from this object you can get an InputStream (and if needed, an OutputStream) so you can print your output or interact with the process.

Martin
  • 3,018
  • 1
  • 26
  • 45
  • I want to pause to see the results in the command prompt (avoid close after it finishes). Does the input stream update in real time? Or after the process has finished? – Mark May 02 '13 at 15:06
  • OK, now it's clear. You would probably have to implement the interaction with the InputStream and OutputStream... The thing is, that with your command, you're opening a new window Process, so there's no way the Java code could wait for the batch file. Unless there's some other way to work with the start command, I think that having the Java code wait for the batch programm termination will requiere you to implement some logic with the streams. – Martin May 03 '13 at 15:10
  • If you want to go the way of interacting from Java with the process, here's an example code: http://www.rgagnon.com/javadetails/java-0014.html – Martin May 03 '13 at 15:11