3

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?

dsiegler19
  • 369
  • 1
  • 5
  • 20
  • 1
    http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java similar to what you are doing – Renuka Deshmukh Mar 03 '16 at 22:24
  • That's what I started with but that would simply give me all of my processes. I try to parse out everything with .app/Contents, but that doesn't work too well. – dsiegler19 Mar 03 '16 at 22:39

1 Answers1

1

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();
    }
}
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • It works perfectly for my uses, but just out of curiosity is there any way to tell what applications are running that are on the dock (i.e. not the background helper apps). – dsiegler19 Mar 04 '16 at 00:25
  • I did try to address that when I wrote it, but don't think it's possible from Java, hence the heuristics. Perhaps there's a Darwin API call or a .plist that lists the Dock contents? Unlikely to be an exact science, but I'll edit answer if I find anything better. – Andrew Regan Mar 04 '16 at 00:33