4,519,128 th visitor since 2017.2.1 ( Today : 9695 )
Programming
No. 765
Name. swindler
Subject. java Runtime InputStream 에서 결과를 가져오지 못할때.
Main Cate. Java
Sub Cate.
Date. 2014-02-25 11:07
Hit. 4476 (211.36.27.3)
File.
grep, ps 등과 같이 pipe로 결과가 나오는 경우에
InputStream 에서 결과를 제대로 가져오지 못한다.

이 경우에는 bash -c 로 실행하여, 다음과 같이 하면 결과를 가져올 수 있다.


Process p = Runtime.getRuntime().exec(new String[] {"bash", "-c", cmd});
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line = in.readLine())!=null) {
out.println(line + "<br>");
}
in.close();

p.destory();

[바로가기 링크] : http://coolx.net/cboard/develop/765



swindler Runtime.exec를 실행하면 결과를 기다리지 않는데, 본문처럼 하면 실행이 끝날때까지 기다릴 수 있다. 2014-06-18
swindler p.destroy()를 하지 않은 경우에, 일부 tomcat에서 lsof 로 검색하면 open된 pipe가 계속 쌓여가는듯 하다. 2014-07-15
Name
Password
Comment
Copyright © 1999-2017, swindler. All rights reserved. 367,611 visitor ( 1999.1.8-2004.5.26 ), 2,405,771 ( -2017.01.31)

  2HLAB   2HLAB_Blog   RedToolBox   Omil   Omil_Blog