0

我遇到问题,在调用 cmd.exe 后,程序显示输出为

运行:成功 Microsoft Windows [版本 6.1.7600] 版权所有 (c) 2009 Microsoft Corporation。版权所有。

C:\Users\prodesign\Documents\NetBeansProjects\Procs>

>之后它只是挂起它试图通过OutputStreamWriter发送“exit\n”字符串但没有运气java的代码如下

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);     

    try
{
  int exitVal;
  char c;
  String ch="exit\n";
 Process process = Runtime.getRuntime().exec("cmd.exe");
  //"cmd /c C:/hello.exe",}
 OutputStream stdin = process.getOutputStream ();
 InputStream stderr = process.getErrorStream ();
 InputStream stdout = process.getInputStream ();

 BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
 System.out.println("sucesss");
 do{ c=(char)reader.read();
 System.out.print(c);

 }while(c!='>');

writer.write(ch);

exitVal=process.waitFor();
System.out.println("Exited with error code "+exitVal);

 }catch (Exception e)
 {
 e.printStackTrace();
 }
}
}

谢谢

4

1 回答 1

0

尝试这个:

 writer.write("exit");
 writer.newLine();

我认为“\n”不起作用。它不会发送命令,直到您刷新流或发送 newLine(); 命令。

于 2013-05-25T09:04:09.717 回答