You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java获取Windows系统中所有已安装应用的可启动引用?

Getting Application Launch Paths Alongside Installed Names

It looks like you've nailed getting the display names of installed apps via PowerShell, but you're missing the critical launch references to actually start those apps. Here's how to bridge that gap, combining registry data and your Java recursive search logic:

The registry key you're querying stores way more than just app names. Add properties like InstallLocation, DisplayIcon, and UninstallString to pull paths that lead directly to the app's executable:

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    Where-Object { $_.DisplayName -ne $null } |
    Select-Object DisplayName, InstallLocation, DisplayIcon, UninstallString
  • DisplayIcon: Often points straight to the app's executable (may include quotes or command-line parameters, so we'll clean that up next).
  • InstallLocation: The root directory where the app is installed—ideal for your recursive search if the icon path isn't available.

Clean Up the DisplayIcon Path

Many DisplayIcon values wrap the exe path in quotes or append extra parameters. Use this snippet to extract the raw executable path:

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    Where-Object { $_.DisplayName -ne $null } |
    ForEach-Object {
        $launchPath = $null
        if ($_.DisplayIcon) {
            # Remove surrounding quotes and split at the first space to drop extra parameters
            $launchPath = $_.DisplayIcon -replace '^"|"$', '' -split ' ', 2 | Select-Object -First 1
        }
        [PSCustomObject]@{
            DisplayName = $_.DisplayName
            LaunchPath = $launchPath
            InstallDir = $_.InstallLocation
        }
    }

If the registry doesn't provide a direct launch path, use your recSearch method to scan the app's InstallDir for executable files. Here's a refined version of your code with checks for common executable extensions:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AppSearch {
    static List<File> Applications = new ArrayList<>();

    public static void recSearch(String path) {
        File root = new File(path);
        // Skip invalid or inaccessible directories
        if (!root.exists() || !root.isDirectory() || !root.canRead()) {
            return;
        }

        File[] list = root.listFiles();
        if (list == null) return;

        for (File file : list) {
            if (file.isDirectory()) {
                recSearch(file.getAbsolutePath());
            } else {
                String fileName = file.getName().toLowerCase();
                // Check for common executable types
                if (fileName.endsWith(".exe") || fileName.endsWith(".bat") || fileName.endsWith(".cmd")) {
                    Applications.add(file);
                }
            }
        }
    }

    // Example usage: Pass the InstallDir from your PowerShell output here
    public static void main(String[] args) {
        recSearch("C:\\Program Files\\YourInstalledApp");
        Applications.forEach(app -> System.out.println("Found executable: " + app.getAbsolutePath()));
    }
}

3. Handle Edge Cases

  • Some apps won't have InstallLocation or DisplayIcon set. For these, fall back to scanning common directories like C:\Program Files or C:\Program Files (x86).
  • Avoid using UninstallString directly for launching apps—it's designed for uninstallation and may include flags that aren't suitable for starting the app normally.

内容的提问来源于stack exchange,提问作者HexadiemsnionalerAlp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:15:00