Created
August 20, 2013 20:28
-
-
Save Kurt-P/6286836 to your computer and use it in GitHub Desktop.
Checks if a process is running or not. Works on both Unix/Linux as well as Windows.
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.InputStreamReader; | |
/** | |
* @author Kurt P | |
* @version 2.5.12272012 | |
*/ | |
public class ProcessChecker { | |
/** | |
* Checks if a specific process is running | |
* | |
* @param String application. The application to check for (Must have ".exe" at the | |
* end to distinguish applications which start with the same name.) | |
* @return true if process is found | |
*/ | |
public boolean check(String application) { | |
//TODO: MAKE THIS DO A SWITCH STATEMETN INSTEAD OF A 4 IF STATEMENTS. | |
if (checkOS().contains("Windows")) { | |
try { | |
String line; | |
Process p = Runtime.getRuntime().exec(System.getenv("windir") | |
+ "\\system32\\" + "tasklist.exe"); | |
BufferedReader input = new BufferedReader( | |
new InputStreamReader(p.getInputStream())); | |
while ((line = input.readLine()) != null) { | |
if (line.contains(application)) { //Parsses the line | |
return true; | |
} | |
} | |
input.close(); | |
} | |
catch (Exception err) { | |
Outputter.getInst().error(err); | |
} | |
return false; | |
} | |
else if (checkOS().contains("Linux")) { | |
try { | |
String line; | |
Process p = Runtime.getRuntime().exec("ps -e"); | |
BufferedReader input = new BufferedReader( | |
new InputStreamReader(p.getInputStream())); | |
while ((line = input.readLine()) != null) { | |
if (line.contains(application)) { | |
return true; | |
} | |
} | |
input.close(); | |
} | |
catch (Exception err) { | |
Outputter.getInst().error(err); | |
} | |
return false; | |
} | |
else if (checkOS().contains("Mac OS")) { | |
try { | |
String line; | |
Process p = Runtime.getRuntime().exec("ps -e"); | |
BufferedReader input = new BufferedReader( | |
new InputStreamReader(p.getInputStream())); | |
while ((line = input.readLine()) != null) { | |
if (line.contains(application)) { | |
return true; | |
} | |
} | |
input.close(); | |
} | |
catch (Exception err) { | |
Outputter.getInst().error(err); | |
} | |
return false; | |
} | |
else { | |
return false; | |
} | |
} | |
/** | |
* @return A string containing the name of the operating system. | |
*/ | |
private String checkOS() { | |
String os; | |
os = System.getProperty("os.name"); | |
return os; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment