Please excuse, this is my first time asking a question. I am looking to find a way using Java to print all of the applications currently running on my computer.
For example:
Google Chrome
Microsoft Word
Microsoft Outlook
Netbeans 8.0.2
Etc.
Currently, I am starting a new process and running the command ps -e then parsing the output. Although I think I am on the right track by using the command line, I think I need a different command. Here is my code:
try {
String line;
Process p = Runtime.getRuntime().exec("ps -e");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
for(int i = 0; i < line.length(); i++){
try{
if(line.substring(i, i + 13).equals(".app/Contents")){
//System.out.println(line.substring(i - 5, i + 3));
int j = 0;
String app = "";
while(!(line.charAt(i + j) == '/')){
app = app + line.charAt(i + j);
//System.out.print(line.charAt(i + j));
j--;
}
String reverse = new StringBuffer(app).reverse().toString();
System.out.println(reverse);
//System.out.println("");
}/*System.out.println(line.substring(i, i + 13));*/}catch(Exception e){}
}
//System.out.println(line); //<-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
So, is this the correct approach and is there just a different command I need to use or is there a better approach overall?