-
[Java] PID 추출Developer/Java 2013. 8. 12. 16:12
String processname="firefox"; // 가져올 프로세스
ProcessBuilder pb2 = new ProcessBuilder("ps" ,"-e");
Process myproc = pb2.start();
InputStream errorOutput = new BufferedInputStream(myproc.getErrorStream(), 1000000);
InputStream consoleOutput = new BufferedInputStream(myproc.getInputStream(), 1000000);
int ch;
String errorlist ="";
while ((ch = errorOutput.read()) != -1) {
errorlist = errorlist + (char) ch;
}
System.out.println("errorlist=="+errorlist);
String processlist = "";
while ((ch = consoleOutput.read()) != -1) {
processlist = processlist + (char) ch;
}
System.out.println("processlist=="+processlist);
String []processarr = processlist.split("\n");
String processtemp = "";
String pid ="";
ArrayList<String> al = new ArrayList();
String getprocessname = "";
for( int j = 0; j< processarr.length;j++){
processtemp = processarr[j];
if(processtemp.length() < 2 )continue;
getprocessname= processtemp.substring(24, processtemp.length());
if( getprocessname.equals(processname)){
pid = processtemp.substring(0, 5);
pid = pid.trim();
System.out.println( "pid =="+ pid);
al.add(pid);
}
}
int exitCode = myproc.waitFor();[출처] [JAVA] 프로세스 PID 가져오기(Linux)|작성자 alucard99
'Developer > Java' 카테고리의 다른 글
[Java] NIO 설명 (0) 2013.08.14 [Java] ThreadLocal 사용법과 활용 (0) 2013.08.14 자바 기초 - 누군가를 위한..ㅋ (0) 2013.05.07 2013 코드잼 - Problem C. (0) 2013.04.19 2013 코드잼 - Problem B. (0) 2013.04.16