Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Last active January 16, 2020 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomoyamkung/6207626 to your computer and use it in GitHub Desktop.
Save tomoyamkung/6207626 to your computer and use it in GitHub Desktop.
[Java]プロセスが起動しているかを確認するクラス
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
/**
* プロセスの起動を監視するクラス。
*
* @author tomoyamkung
*
*/
public class ProcessWatch {
private static Logger log = Logger.getLogger(ProcessWatch.class);
private static final String TASKLIST = "tasklist";
/** Excel のプロセス名 */
public static final String EXCEL_EXE = "EXCEL.EXE";
/**
* プロセスが起動しているかを問い合わせる。
*
* @param processName プロセス名
* @return
*/
public static boolean isRun(String processName) {
try {
Process p = new ProcessBuilder(ProcessWatch.TASKLIST).start();
BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
try {
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
if(line.startsWith(processName)) {
return true;
}
}
} finally {
br.close();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
e.printStackTrace();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment