mcfly soft
mcfly soft

Reputation: 11665

how to hide cmd window when executing cmd with Runtime.getRuntime().exec

I would like to execute multiple commands in a cmd shell from java: sample:

String cmdShell = "cmd /c start cmd.exe /K ";
String endCommand = cmdShell + "\"" + multiplecommands + " && exit" + "\"";
Process proc = Runtime.getRuntime().exec(endCommand);
final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
    LOGGER.debug("" + line);
}
proc.waitFor();

This opens the black window and closes after finished. Is there a way to hide this window. Or any other way to execute multiple commands without showing the cmd window ?

Upvotes: 1

Views: 764

Answers (2)

Istiaque Hossain
Istiaque Hossain

Reputation: 2367

you can try this code , in my case this code give all Directory of C:\xampp folder in my console ...without open CMD

public static void main(String[] args)throws Exception {
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd C:\\xampp && C: && dir");
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) {
            break;
        }
        System.out.println(line);
    }
}

for more study you can read this page

Upvotes: 1

Anatoly
Anatoly

Reputation: 66

Maybe it useful "start" with "/min":

start /min .....
..........
exit

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start

Upvotes: 1

Related Questions