これらのことを異なるスレッドで非同期に実行したい場合があり、例外やエラー ストリームを無視したくないことは間違いありません。
しかし、最も重要なのは、「cmd /c start cmd.exe」を実行することでプロセス内にプロセスを作成しているため、cmd を正しく呼び出していないことです。
たとえば、
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();
}
}
}