- - PR -
ProcessクラスのStreamが欠ける現象
1
| 投稿者 | 投稿内容 |
|---|---|
|
投稿日時: 2002-12-05 18:26
お世話になっております。松崎です。
UNIX系のシステムとのWEBシステムの連携で、一つテストしている ソースがあるのですが上手く動きません。API仕様書を読んでみたの ですが、確かにProcessクラスはネイティブなプラットフォームとの 連携が上手くいかない?という様な記述が見うけられます。ですが、 その内容が理解できていません。 現象としてはProcessクラスからのStreamを読みこんだ時、結果が 歯抜けになってしまうというものです。例えば本来psコマンドで root 961 1 0 Nov25 tty1 00:00:00 /sbin/mingetty tty1 root 962 1 0 Nov25 tty2 00:00:00 /sbin/mingetty tty2 root 963 1 0 Nov25 tty3 00:00:00 /sbin/mingetty tty3 root 964 1 0 Nov25 tty4 00:00:00 /sbin/mingetty tty4 root 965 1 0 Nov25 tty5 00:00:00 /sbin/mingetty tty5 root 966 1 0 Nov25 tty6 00:00:00 /sbin/mingetty tty6 上記結果がでなければならないものが、Processクラスで読みこむと root 961 1 0 Nov25 tty1 00:00:00 /sbin/mingetty tty1 root 963 1 0 Nov25 tty3 00:00:00 /sbin/mingetty tty3 root 964 1 0 Nov25 tty4 00:00:00 /sbin/mingetty tty4 root 966 1 0 Nov25 tty6 00:00:00 /sbin/mingetty tty6 上記の様な結果となってしまいます。何か同様の現象をご存知でしたら ご教授下さい。よろしくお願い致します。 ソースは以下です。 public String logic() throws IOException { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("ps -ef"); br = new BufferedReader(new InputStreamReader( proc.getInputStream())); sw = new StringWriter(); pw = new PrintWriter(sw); Thread th = new Thread(this); th.start(); try{ th.join(); }catch (InterruptedException ex){ } br.close(); pw.close(); return sw.toString(); } public void run() { String line; try{ while ((line = br.readLine()) != null) { pw.println(line); br.readLine(); } }catch (IOException ex) { } } |
|
投稿日時: 2002-12-05 18:55
readLineをwhileの条件節と、whileブロック中それぞれ1回ずつ
呼んでるから、1ループあたりreadLineが2回呼ばれますよ。 while ((line = br.readLine()) != null) { pw.println(line); } |
|
投稿日時: 2002-12-06 10:31
ありがとうございます。
サンプルをそのまま使ってみたのですが、見落としていました。 まずは取り除いてもう一度テストしてみます。 |
1
