6

How to get list of current running application like Task Manager except applications running in background using JAVA? I have this code i found - https://stackoverflow.com/a/41634959/11297873:

 Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
    new Thread(() -> {
        Scanner sc = new Scanner(process.getInputStream());
        if (sc.hasNextLine()) sc.nextLine();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] parts = line.split(",");
            String unq = parts[0].substring(1).replaceFirst(".$", "");
            String pid = parts[1].substring(1).replaceFirst(".$", "");
            System.out.println(unq + " " + pid);
        }
    }).start();
    process.waitFor();
    System.out.println("Done");

I do not want to show background application like System, Windows, Intel etc....

Newbie Coder
  • 141
  • 1
  • 11

2 Answers2

3

You need here the powershell cmdlet called Get-Process.

   Process process = new ProcessBuilder("powershell","\"gps| ? {$_.mainwindowtitle.length -ne 0} | Format-Table -HideTableHeaders  name, ID").start();
        new Thread(() -> {
            Scanner sc = new Scanner(process.getInputStream());
            if (sc.hasNextLine()) sc.nextLine();
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                System.out.println(line);
            }
        }).start();
    process.waitFor();
    System.out.println("Done");

The output would be like this:

ApplicationFrameHost 15592
chrome               12920
cmd                  21104
debian               13264
Far                   3968
firefox              17240
HxOutlook             4784
idea64               13644
MicrosoftEdge         8024
MicrosoftEdgeCP      13908
MicrosoftEdgeCP      14604
mstsc                24636
notepad++             9956
OUTLOOK               9588
pycharm64             6396
rider64              10468
Teams                11540
Telegram             16760


Done
  • 1
    how about getting the list of running application in MAC, Unix, Ubuntu etc ...? – Newbie Coder Apr 02 '19 at 23:58
  • Unfortunately, there is no common solution for all Linux Platforms, it depends on a windowing system that a Platform uses.Eg. Here what I find for [Ubuntu](https://askubuntu.com/questions/728157/how-to-get-the-list-of-running-gui-applications-in-the-unity-launcher). – Oleg Ashchepkov Apr 03 '19 at 04:07
  • @NewbieCoder did you find solution for mac ? – Ashwin Nirmale Sep 28 '20 at 10:27
1

So i took your code

Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
    new Thread(() -> {
        Scanner sc = new Scanner(process.getInputStream());
        if (sc.hasNextLine()) sc.nextLine();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] parts = line.split(",");
            String unq = parts[0].substring(1).replaceFirst(".$", "");
            String pid = parts[1].substring(1).replaceFirst(".$", "");
            System.out.println(unq + " " + pid);
        }
    }).start();
    process.waitFor();
    System.out.println("Done");

And changed it to

Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
    new Thread(() -> {
        Scanner sc = new Scanner(process.getInputStream());
        if (sc.hasNextLine()) sc.nextLine();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] parts = line.split(",");
            String unq = parts[0].substring(1).replaceFirst(".$", "");
            String pid = parts[1].substring(1).replaceFirst(".$", "");
            System.out.println(line);
        }
    }).start();
    process.waitFor();
    System.out.println("Done");

essentially printing the entire line instead of just "unq" and "pid"

what i saw was that the entire printed line looked like this:

"System Idle Process","0","Services","0","8 K"

first 2 strings beeing "unq" and "pid" , while 3rd string is the specification of the process and the 5th string is how much memory it uses.

For what i could tell it seems like the 4th string might be what you are looking for. it always is either 0 or 1 and from what i could tell foreground processes always had a 1 therefore i assume if you check for it to be 0 you can get your background processes like this:

Process process = new ProcessBuilder("tasklist.exe", "/fo", "csv", "/nh").start();
    new Thread(() -> {
        Scanner sc = new Scanner(process.getInputStream());
        //if (sc.hasNextLine()) sc.nextLine();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] parts = line.split(",");
            String unq = parts[0].substring(1).replaceFirst(".$", "");
            String pid = parts[1].substring(1).replaceFirst(".$", "");
            if(parts[3].substring(1).replaceFirst(".$", "").equals("0")) {
                System.out.println(unq + " " + pid);
            }

        }
    }).start();
    process.waitFor();
    System.out.println("Done");

still not 100% sure and i couldnt find much by googling. but so far it seems like this could be the case

Alan
  • 949
  • 8
  • 26