0
private static void runner() {
    try
    {
        Runtime rt = Runtime.getRuntime();

        //Runtime rt = Runtime();
        //rt.getRuntime();

        String command = "C:\\dummy.exe";
        //command = "cmd";
        Process pr = rt.exec(command);

        BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
        String commandToSend;
        //commandToSend = "dir\n";

        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.println("Enter a command");
        commandToSend = reader.nextLine(); // Scans the next token of the input as an int.

        System.out.println(commandToSend);
        processInput.write(commandToSend+"\r\n");
        processInput.flush();
        int lineCounter = 0;
        String line ="";
        boolean go2 =true;
        while(go2)
        {
               line = processOutput.readLine();
        //System.out.println("2");  

            if(line == null) {
                System.out.println("\n\ndone\n");
                go2=false;
            }


            //System.out.println(go2);
            System.out.println(++lineCounter + ": " + line); 
        }
        }
        reader.close();
        processInput.close();
        processOutput.close();
        pr.waitFor();
        System.out.println("done\n");
    }
    catch(Exception x)
    {
        x.printStackTrace();
    }
}

that is the output

Enter a command
dir
dir

3: 
12: 02/03/2018  02:35 PM               301 .classpath
13: 02/03/2018  02:35 PM               382 .project
14: 02/03/2018  02:35 PM    <DIR>          .settings
15:
16: 02/03/2018  02:36 PM    <DIR>          src
17:                2 File(s)            683 bytes
18:               5 Dir(s)  702,993,940,480 bytes free
19: 

it freezes here ^ it freezes at the line line =processOutput.readline() I have tried multiple print statements and found that after line 19 it freezes but doesnt throw an error. I think the issue has to deal with how buffered nulls I have snipped some of the output which explains the gap in numbers but the main thing to illustrate is that it does not freeze on newlines just at the end

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Chawn Neal
  • 19
  • 2
  • 2
    Yes, it's probably waiting for more input (or output from the process as the case may be) – MadProgrammer Feb 04 '18 at 21:10
  • I'd recommend having a read through [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) and maybe even [this example](https://stackoverflow.com/questions/11893469/java-execute-a-program-with-options-like-ls-l/11893495#11893495) for some basic ideas – MadProgrammer Feb 04 '18 at 21:21
  • Now, [this example](https://stackoverflow.com/questions/32343355/java-linux-terminal-in-jpanel/32343778#32343778) is probably a little beyond what you're looking for, but it demonstrations a concept of interacting with a running process – MadProgrammer Feb 04 '18 at 21:22
  • It *blocks,* waiting for more process output. Try closing the process's input. – user207421 Feb 04 '18 at 21:25
  • ok thanks for responding so quickly and i will read through your link but just looking at what im dealing with. how can I escape the loop or tell when the input stream ends. This is a smaller portion of a larger project. – Chawn Neal Feb 04 '18 at 21:26
  • Im reading the example now, but i should also mention my intention is to change the input from scanner and it is also not to create a terminal window like theirs does. – Chawn Neal Feb 04 '18 at 21:29
  • ohh thank you at EJP i adjusted and got a modified output – Chawn Neal Feb 04 '18 at 21:32
  • I will post the edit.But now I will check to see new issues. – Chawn Neal Feb 04 '18 at 21:33

0 Answers0