How can I unblock from a Java started process?

前端 未结 8 1085
旧巷少年郎
旧巷少年郎 2021-01-29 01:27

When executing some command(let\'s say \'x\') from cmd line, I get the following message: \"....Press any key to continue . . .\". So it waits for user input to unblock.

8条回答
  •  故里飘歌
    2021-01-29 01:56

    I had the same problem and I found a solution. It ins´t the most elegant, but it works.

    1 - when you execute the process, you get the inputStream from the process 2 - Then you make a loop receiving the message shown in the prompt, if there was one 3 - When you see that you got from "prompt" the "press a key to continue", or whatever, you end the proccess

                // Creates the runtime and calls the command
            Process proc = Runtime.getRuntime().exec(Destino);
    
            // Get the proccess inputStream
            InputStream ips = proc.getInputStream();
            String output = "";
    
            int c = 0;
    
            // Read the output of the pro
            while ((c = ips.read()) != -1
                    && !output.contains("Press any key to continue")) {
                output = output + (char)c;
            }
    
            // Destroy the proccess when you get the desired message
            proc.destroy();
    

提交回复
热议问题