dsiegler19
dsiegler19

Reputation: 499

Printing all running applications Java

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?

Upvotes: 3

Views: 387

Answers (1)

Andrew Regan
Andrew Regan

Reputation: 5113

This is hardly optimised, but produces a plausible-looking list. The heuristic to exclude anything under /System/ or /Library/ etc. seems to produce good results, but it's up to you. It really depends what you want to do with the list, whether it's something you hope to put in front of a user.

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Program {

    public static void main(String[] args) throws IOException {

        final Pattern APP_PATTERN = Pattern.compile("\\/([^/]*)\\.app\\/Contents");

        Set<String> apps = new TreeSet<>();

        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(" /System/") &&
                !line.contains("/Library/") &&
                !line.contains("/Application Support/")) {
                Matcher m = APP_PATTERN.matcher(line);
                if (m.find()) {
                    apps.add( m.group(1) );
                }
            }
        }
        System.out.println("Apps: " + apps);
        input.close();
    }
}

Upvotes: 1

Related Questions