1

Java プログラムから Linux コマンド ラインを実行した後、エラー出力を System.out.println に出力する必要があります。

だからここに私のコードがあります:

try {
  Process proc =  Runtime.getRuntime().exec(xmlcommand, null, new File(servlet.getInitParameter(Constants.WORKING_DIR)));
  outThread = new StreamReaderThread(proc.getInputStream(), System.out);
  errThread = new StreamReaderThread(proc.getErrorStream(), System.err);
  outThread.start();
  errThread.start();
  proc.waitFor();
  //finish reading whatever's left in the buffers
  outThread.join();
  errThread.join();

 // Read from an input stream
 String line; 
 BufferedReader input = new BufferedReader(new InputStreamReader( proc.getErrorStream()));
  line = input.readLine(); 

  while(line != null) 
  { 
      System.out.println(line); 
      line = input.readLine(); 
  } 
} catch (IOException e) {
 // new Notification(e.getMessage(), Notification.Type.ERROR_MESSAGE).show(ui.getPage().getCurrent());
  e.printStackTrace();
} catch (InterruptedException e) {
  new Notification(e.getMessage(), Notification.Type.ERROR_MESSAGE).show(ui.getPage().getCurrent());
}

しかし、実行後にこのエラーが発生します:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.read(BufferedInputStream.java:325)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
...
4

2 に答える 2

1

エラー出力を出力する前に、実行スレッドを強制的に待機させる必要があります

だからこれを試してください:

//finish reading whatever's left in the buffers
outThread.join();
errThread.join();

// Read from an input stream
String line; 
BufferedReader input = new BufferedReader(new InputStreamReader(  proc.getErrorStream()));
line = input.readLine(); 

while(line != null) 
{ 
  System.out.println(line); 
  line = input.readLine(); 
} 

proc.waitFor();

 } catch (IOException e) {
 // new Notification(e.getMessage(),       
 Notification.Type.ERROR_MESSAGE).show(ui.getPage().getCurrent());
 e.printStackTrace();
 } catch (InterruptedException e) {
   new Notification(e.getMessage(),    
   Notification.Type.ERROR_MESSAGE).show(ui.getPage().getCurrent()); 
  }
于 2013-10-29T10:05:34.147 に答える