How can I unblock from a Java started process?

前端 未结 8 1053
旧巷少年郎
旧巷少年郎 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:53

    I think (although can't be certain) that you're talking about Windows rather than Unix?

    If so, it's possible that the command line process isn't actually waiting for a key press (or input) on stdin but instead doing the equivalent of the old DOS kbhit() function.

    AFAIK there's no way to make that function believe that the keyboard has been pressed without actually pressing a key.

    To test this theory, create a text file "input.txt" with some blank lines in it, and run:

    foo.exe < input.txt
    

    That will show whether your program is waiting on stdin or on something else.

    0 讨论(0)
  • 2021-01-29 01:54

    You should read the ouput and error streams of your subprocess simultaneously. The buffer size of these streams is limited. If one of the buffers gets full the subprocess will block. I think that is what happens in your case.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2021-01-29 02:03

    So this is my workaround to this problem, inspired from Alnitak's suggestion: Run the command like this:

    Process p = Runtime.getRuntime().exec(cmd + " < c:\\someTextFile.txt", null, cmdDir);
    ...
    int errCode = p.waitFor();
    ...
    

    the 'someTextFile.txt' can be programatically created into the temporary dir then deleted.

    0 讨论(0)
  • 2021-01-29 02:04

    I wrote an answer to command line execution at this stackoverflow question.

    Yours is a bit more tricky since you probably need to reply.

    In your case it might me necessary to give the input stream gobbler something like a reply channel:

     StreamGobbler outputGobbler = new StreamGobbler(
                                        proc.getInputStream(), "OUTPUT", 
                                        proc.getOutputStream());
    

    and make it match a pattern and reply on the given input stream.

    while ((line = br.readLine()) != null) {
        System.out.println(type + ">" + line);
        if (line.contains(PRESS_KEY_CONTINUE) {
            ostream.write("y".getBytes("US-ASCII")); 
            System.out.println("< y"); 
        }
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-29 02:07

    The program doesn't continue because it is blocked expecting input from the user.

    An option is to launch the outer process in a separate thread, or use threads sharing the Process p in order to be able to write to its stream.

    0 讨论(0)
提交回复
热议问题