How can I run multiple commands in only one CMD window in Java?

what I would like to do is run the batch file several times from a java application. So I installed a for-loop that runs this code n times:

 for (int i = 0; i < n; i++) { Runtime.getRuntime().exec("cmd /c start somefile.bat"); } 

The problem is that every time the command is run, a new cmd window appears. However, what I want is just one window that appears at the beginning and is used to display all the data from the following command calls.

How can i do this?

+7
java for-loop batch-file windows-runtime
source share
2 answers

With &&, you can execute several commands one after another:

 Runtime.getRuntime().exec("cmd /c \"start somefile.bat && start other.bat && cd C:\\test && test.exe\""); 

Using multiple commands and conditional processing characters

You can run multiple commands from the same command line or script using conditional processing characters. When you run several commands with conditional processing symbols, the commands to the right of the conditional processing symbol act on the basis of the results of the command to the left of the conditional processing symbol.

For example, you can run a command only if the previous command fails. Or you can only run the command if the previous command completed successfully. You can use the special characters listed in the following table to send multiple commands.

& [...] command1 & command2
Use to separate multiple commands on the same command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the character is successful. Cmd.exe runs the first command, and then runs the second command only if the first command has completed successfully.

|| [...] command1 || command2
Use to run the command following || only if the command preceding || out of order. Cmd.exe starts the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

( ) [...] (command1 & command2)
Use to group or nest multiple teams.

; or , command1 parameter1;parameter2
Use to separate command options.

+17
source share

I would use Java ProcessBuilder or another class that mimics / uses a shell. The following snippet demonstrates the idea (for Linux with bash).

 import java.util.Scanner; import java.io.*; public class MyExec { public static void main(String[] args) { //init shell ProcessBuilder builder = new ProcessBuilder( "/bin/bash" ); Process p=null; try { p = builder.start(); } catch (IOException e) { System.out.println(e); } //get stdin of shell BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); // execute the desired command (here: ls) n times int n=10; for (int i=0; i<n; i++) { try { //single execution p_stdin.write("ls"); p_stdin.newLine(); p_stdin.flush(); } catch (IOException e) { System.out.println(e); } } // finally close the shell by execution exit command try { p_stdin.write("exit"); p_stdin.newLine(); p_stdin.flush(); } catch (IOException e) { System.out.println(e); } // write stdout of shell (=output of all commands) Scanner s = new Scanner( p.getInputStream() ); while (s.hasNext()) { System.out.println( s.next() ); } s.close(); } } 

Please note that this is just a fragment that needs to be adapted for Windows, but in general it should work with cmd.exe .

+10
source share

All Articles