2014-11-04 12 views
0

에있는 파일에 기록하지, 내가 외부 프로그램에 대한 호출이, 나는 다음과 같은 사용출력 내 자바 코드 내에서 자바

int returnCodeC = 0; 
String cmnd = "dia -fa -fn res" + file1; 
final Process processCC = Runtime.getRuntime().exec(cmnd); 
BufferedReader bufC = new BufferedReader(new InputStreamReader(processCC.getInputStream())); 
returnCodeC = processCC.waitFor(); 
    } catch (Exception e1) {e1.printStackTrace();}} 

외부 프로그램 직경에 대한 호출의 출력 file1이라는 파일에 작성해야합니다. 오늘까지 정상적으로 작동하고있었습니다. 문제는 dia에서 출력이 커지면 출력이 나오지 않는다는 것입니다. BufferReader에 대한 크기 제한이 있습니까?

+0

String [] cmd = {"dia", "-fa", "-fn", "res"};을 사용하여 수신 : 여기에 당신을 도울 수있는 링크입니다? 버퍼 제한이 아닐 수도 있습니다. –

+0

터미널에서 dia 명령을 실행할 때 모든 것이 잘 작동합니다. 자바에서 호출 할 때도 작동합니다 (dia에 사용되는 입력 파일이 작아서 출력 크기가 작았 음) 지금은 큰 입력 파일과 자바에서 dia를 실행 .. 출력 (큰) 출력 파일에 기록되지 않습니다. – SLA

+0

버퍼 제한 ptoblem 경우 확실하지 않습니다 .. 나는 아직도 문제가 뭔지 모르겠다. – SLA

답변

0

이제 문제가 더 잘 이해되어 processBuilder를 사용하는 것이 좋습니다. 이를 통해 프로세스 출력에 대한 탁월한 제어 권한을 가질 수 있습니다. ProcessBuilder

였는지를 당신이 여전히 디아에서 무엇을 읽고 확신 인자로 문자열 배열이 너무

+0

Runtime.getRuntime(). exec (cmnd); 단일 String에 매우 만족합니다. 어쩌면 그 사용법이 시대에 뒤떨어 졌을 지 모르지만, 이것이 이것이 문제의 원인인지는 의심 스럽습니다. –

+0

@SLA, 약간 혼란 스럽습니다.이 문제만으로도 문제가 해결 되었습니까? 실행 코드를 게시 하시겠습니까? 감사. –

0

이것은 더 많은 의견이지만 포퍼 대답이지만 나쁜 평판은 내가 의견을 게시하는 것을 허용하지 않습니다.

stdout 또는 stderr이 각각의 버퍼 크기를 오버플로하면 프로세스가 차단됩니다.

셸에서 명령을 수행하여 결과를 확인하고 stdterr 및 stdout을 읽는 스레드를 시작하십시오.

편집 : 여기에 내가 그물에서 발견 한 것이고, stdout과 stderr의 내용이 필요하지 않을 때 작동하지만 명령 만 실행할 수 있습니다. processCC.waitFor() 바로 전에 dropOutput을 호출합니다.

private int dropOutput(final Process p) { 

    final Thread t = new Thread("drop") { 

     @Override 
     public void run() { 
     _dropOutput(p); 
     }; 
    }; 

    t.start(); 
    int ev = 0; 
    Timer timer = null; 
    try { 
     timer = new Timer(true); 
     final InterruptTimerTask interrupter = new InterruptTimerTask(Thread.currentThread()); 
     timer.schedule(interrupter, 2 /*seconds*/* 1000 /*milliseconds per second*/); 
     if (p.waitFor() != 0) { 
     ev = p.exitValue(); 

     } 
    } catch (final InterruptedException e) { 
     // e.printStackTrace(); 
    } finally { 
     timer.cancel(); // If the process returns within the timeout period, we have to stop the interrupter 
         // so that it does not unexpectedly interrupt some other code later. 

     Thread.interrupted(); // We need to clear the interrupt flag on the current thread just in case 
          // interrupter executed after waitFor had already returned but before timer.cancel 
          // took effect. 
          // 
          // Oh, and there's also Sun bug 6420270 to worry about here. 
    } 
    return ev; 
    } 

    /** 
    * Just a simple TimerTask that interrupts the specified thread when run. 
    */ 
    class InterruptTimerTask extends TimerTask { 

    private final Thread thread; 

    public InterruptTimerTask(final Thread t) { 
     this.thread = t; 
    } 

    @Override 
    public void run() { 
     thread.interrupt(); 
    } 

    } 

    private void _dropOutput(final Process p) { 
    final InputStream istrm = p.getInputStream(); 
    // final InputStream istrm = p.getErrorStream(); 
    final InputStreamReader istrmrdr = new InputStreamReader(istrm); 
    final BufferedReader buffrdr = new BufferedReader(istrmrdr); 

    @SuppressWarnings("unused") 
    String data; 
    try { 
     while ((data = buffrdr.readLine()) != null) { 
     // System.out.println(data); 
     } 
    } catch (final IOException e) { 
     // do nothing 
    } 
    } 
+0

그것은 쉘에서 작동합니다 .. 제 코멘트를 읽어 주실 수 있습니까? 어떻게하면 해결할 수 있습니까? – SLA

+0

다행히도 나는 적어도 내 대답에 대해 논평 할 권한이 있습니다. 셸에서 명령을 실행하면 출력이 얼마나 큽니까? –