2011-09-12 3 views
2

java에서 일부 파일을 실행하기 위해 mysql을 실행하려고합니다. 입력 파일에서 읽어와 MySQL의 프로세스에 파이프로 연결하여, 모든게 괜찮아 보이지만 라인waitFor()에서 프로세스가 멈 춥니 다.

int exitCode = proc.waitFor(); 

는 결코 반환하지 않습니다.

private boolean runScript(String path, String cmd, File file) throws IOException, InterruptedException { 
     Runtime rt = Runtime.getRuntime(); 
     Process proc = rt.exec(path + File.separatorChar + cmd); 
     OutputStream procOS = proc.getOutputStream(); 
     InputStream procES = proc.getErrorStream(); 
     InputStream procIS = proc.getInputStream(); 
     OutputStreamWriter procStdIn = new OutputStreamWriter(procOS); 

     FileInputStream fis = new FileInputStream(file); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 
     String send; 

     while ((send = reader.readLine()) != null) { 
      procStdIn.write(send); 
      System.out.println(send); 
      copyStream(procES, System.err); 
      copyStream(procIS, System.out); 
     } 
     procStdIn.write("\r\nexit\r\n"); 
     int exitCode = proc.waitFor(); 
     return exitCode == 0; 
    } 

    private void copyStream(InputStream is, PrintStream err) throws IOException { 
     byte b[] = new byte[ 1024 ]; 
     int length; 
     while (is.available() > 0 && (length = is.read(b)) > 0) { 
      err.write(b, 0, length); 
     } 

}

+0

당신은 자식 프로세스가 종료 된 것을 볼 수 있습니까? 아마도'copyStream'은'exit'를 쓰고 난 후 마지막으로 한 번해야할까요? –

+0

@Hemal 아직 카드 프로세스가 실행되고 있지 않습니다. – stacker

답변

3

난 당신이 표준 출력과 표준 에러를 읽고 차단하고 의심한다. 자세한 내용은 this answer을 참조하십시오.

+0

죄송합니다. avaliable() 호출로 인해 copyStream 메서드가 차단되지 않습니다. – stacker

+1

감사합니다 문제는 mysqls 입력 스트림에서 flush()가 누락되었습니다. – stacker

0

추가 :

procStdIn.flush(); 

후 :

procStdIn.write("\r\nexit\r\n"); 
+0

코드를 작성하는 대신 설명 할 수 있습니까? – eirikir

관련 문제