Last active
January 16, 2020 11:26
-
-
Save tomoyamkung/6207626 to your computer and use it in GitHub Desktop.
[Java]プロセスが起動しているかを確認するクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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