如何用Java获取Windows系统中所有已安装应用的可启动引用?
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:
1. Expand Your PowerShell Command to Capture Launch-Related Data
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 } }
2. Integrate with Your Java Recursive Search
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
InstallLocationorDisplayIconset. For these, fall back to scanning common directories likeC:\Program FilesorC:\Program Files (x86). - Avoid using
UninstallStringdirectly for launching apps—it's designed for uninstallation and may include flags that aren't suitable for starting the app normally.
内容的提问来源于stack exchange,提问作者HexadiemsnionalerAlp

