どのようにJAVAを使用してバックグラウンドで実行中のアプリケーションを除いて、タスクマネージャのように、現在実行中のアプリケーションのリストを取得するには?

初心者コーダー:

どのようにJAVAを使用してバックグラウンドで実行中のアプリケーションを除いて、タスクマネージャのように、現在実行中のアプリケーションのリストを取得するには?私は、私が見つけたこのコードを持っている- 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");

私は....システム、Windowsの、インテルなどのようなバックグラウンドアプリケーションを表示したくありません

オレグAshchepkov:

あなたは、PowerShellのコマンドレットと呼ばれるここで必要-プロセスを取得します

   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");

出力は次のようになります:

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=195187&siteId=1