Error while reading from command prompt using ProcessBuilder
<p>I'm having an issue while executing the java code by using ProcessBuilder.</p>

<p>Below are the scenarios of the code where i have been facing the error.</p>

<p><strong>Scenario-1</strong></p>

<pre><code>String[] commands = {"cd C:\\glm","glm"};

    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(commands);

    InputStream stdin = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stdin);
    BufferedReader br = new BufferedReader(isr);

    String line = null;
    System.out.println("&lt;OUTPUT&gt;");

    while ( (line = br.readLine()) != null)
         System.out.println(line);
</code></pre>

<p>the <strong><em>error</em></strong> after executing the code is </p>

<pre><code>    Exception in thread "main" java.io.IOException: Cannot run program "cd C:\glm": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at java.lang.Runtime.exec(Runtime.java:617)
    at java.lang.Runtime.exec(Runtime.java:485)
    at com.glm.GLM_Command.main(GLM_Command.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.&lt;init&gt;(ProcessImpl.java:385)
    at java.lang.ProcessImpl.start(ProcessImpl.java:136)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
</code></pre>

<p>after the above code, i tried with the following code</p>

<p><strong>Scenario-2</strong></p>

<pre><code>File thisFile = new File("C:\\somefile.txt");
        ProcessBuilder pb = new ProcessBuilder("C:\\glm\\glm.exe");
        pb.redirectErrorStream(true);
        pb.redirectError(thisFile);
        pb.start();
</code></pre>

<p>the output for the above code is as shown in the image</p>

<p><a href="https://i.stack.imgur.com/MebLu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MebLu.png" alt="enter image description here"></a></p>

<p><strong>Scenario-3</strong></p>

<p>the tricky part is while executing the command through command prompt:</p>

<p>the output is as displayed in the below image</p>

<p><a href="https://i.stack.imgur.com/gDRuH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gDRuH.png" alt="enter image description here"></a></p>

<p>My task is to read the data which is in the command prompt.</p>

<p>Finally </p>

<p><strong>Scenario-4</strong></p>

<pre><code>public static void main(String[] args) throws Exception 
    {
        Process p = Runtime.getRuntime().exec("C:\\glm\\glm");
        // Process p = Runtime.getRuntime().exec("cmd /c dir");
        inheritIO(p.getInputStream(), System.out);
        inheritIO(p.getErrorStream(), System.err);

    }

    private static void inheritIO(final InputStream src, final PrintStream dest) 
    {
        new Thread(new Runnable() 
        {
            public void run() 
            {
                Scanner sc = new Scanner(src);
                while (sc.hasNextLine()) 
                {
                    dest.println(sc.nextLine());
                }
            }
        }).start();
    }
</code></pre>

<p>the output is shown below</p>

<p><a href="https://i.stack.imgur.com/eGEZw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eGEZw.png" alt="enter image description here"></a></p>

<p>please suggest me where i was wrong.</p>

<p>Note: we have been providing the command from the manual :) .
(if there is no other solution for this, is there any way to read all the values before getting the error in a file for workaround?).</p>