2012-05-28 4 views
1

여기 내 코드는 7 번 명령 프롬프트 창을 연 다음 표시됩니다. 아무것도. 나는 명령을 분명히 보내고 받기를 원한다. 그래서 뭐가 잘못 됐니 ??java 파이프/프로세스 빌더가 cmd.exe와 함께 작동하지 않습니다.

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

답장 수정을 참조하십시오. –

답변

1

서로 다른 스레드에서 비동기 적으로이 일을 수행 할 수 있습니다, 그리고 당신은 확실히 예외 또는 오류 스트림을 무시하지 않습니다.

하지만 가장 중요한 점은 "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(); 
     } 
    } 
} 
+1

스레드 문제가 아닙니다. –

+0

@MK : 정정 됨, 감사합니다. –

+0

이 작동했습니다! 덕분에 그것은 스레딩 문제가되었습니다 – user1058682

2

cmd를 새 명령 프롬프트 창을 사용자의 입력 및 출력 버퍼가 연결되지 않습니다 시작됩니다 시작합니다.