1

here's my code it just opens a command prompt windows 7 and then sits. nothing else. i want it to send and receive commands obviously. so whats wrong??

String line;
try {
    Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
    BufferedReader inp =
        new BufferedReader(
            new InputStreamReader(p.getInputStream()));
    BufferedWriter out =
        new BufferedWriter(
            new OutputStreamWriter(p.getOutputStream()));
    out.append("sometext");
    out.write("Some Text!\n\n");
    out.flush();
    line = inp.readLine();
    System.out.println("response1: " + line );   // that's ok
    out.write("Second Line...\n");
    out.flush();
    line = inp.readLine();
    // returns an empty string, if it returns...
    System.out.println("response2: " + line);    
    inp.close();
    out.close();
} catch (IOException io) {

}
0

2 Answers 2

2

cmd start will launch a new command prompt window and your input and output buffers will not be connected to it.

Sign up to request clarification or add additional context in comments.

Comments

1

You may want to do these things asynchronously on different threads, and you will definitely not want to ignore exceptions or the error stream.

But most important, you're not calling cmd correctly since by doing "cmd /c start cmd.exe" you're creating a process within a process.

For e.g.,

import java.io.*;

public class OpenCmd {
   public static void main(String[] args) {
      try {
         // Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
         Process p = Runtime.getRuntime().exec("cmd.exe");
         final BufferedReader inp = new BufferedReader(new InputStreamReader(
               p.getInputStream()));
         final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
               p.getOutputStream()));
         final BufferedReader err = new BufferedReader(new InputStreamReader(
               p.getErrorStream()));

         new Thread(new Runnable() {
            public void run() {
               try {
                  out.append("sometext");
                  out.write("Some Text!\n\n");
                  out.flush();
                  out.write("Second Line...\n");
                  out.flush();
               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     out.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }).start();
         new Thread(new Runnable() {
            public void run() {
               try {
                  String line = "";
                  while ((line = inp.readLine()) != null) {
                     System.out.println("response1: " + line);
                  }

               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     inp.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }).start();
         new Thread(new Runnable() {
            public void run() {
               try {
                  String line = "";
                  while ((line = err.readLine()) != null) {
                     System.err.println("err: " + line);
                  }
                  inp.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         }).start();
         int exitVal = p.waitFor();
         System.out.println("exitVal := " + exitVal);

      } catch (IOException io) {
         io.printStackTrace();
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}

1 Comment

@user105: Not quite. Threads are an issue, but not the major one. The major one was what you passed in to the exec(...) method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.