Java tutorial
//package com.java2s; /* See LICENSE for licensing information */ import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class Main { public final static String SHELL_CMD_PIDOF = "pidof"; public static int findProcessIdWithPidOf(String command) throws Exception { int procId = -1; Runtime r = Runtime.getRuntime(); Process procPs = null; String baseName = new File(command).getName(); //fix contributed my mikos on 2010.12.10 procPs = r.exec(new String[] { SHELL_CMD_PIDOF, baseName }); //procPs = r.exec(SHELL_CMD_PIDOF); BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { try { //this line should just be the process id procId = Integer.parseInt(line.trim()); break; } catch (NumberFormatException e) { logException("unable to parse process pid: " + line, e); } } return procId; } public static void logException(String msg, Exception e) { } }