首页 文章

如何从Java获取Windows 10上已安装应用程序的列表

提问于
浏览
2

我正在尝试从我的Java程序中获取Windows 10中安装的所有应用程序的列表 . 我尝试过以下方法:

Runtime.getRuntime().exec("Get-WmiObject -class Win32_Product | Select-Object -Property Name");

我得到:

Cannot run program "Get-WmiObject": CreateProcess error=2

我也试过了:

Process p = Runtime.getRuntime().exec("Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize");

有类似的结果 .

最后,我尝试使用库“win32”,但它只返回安装的某些程序的名称 .

我需要在PowerShell中执行以下命令时获得的结果:

Get-ItemProperty HKLM:\ Software \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ * | Select-Object DisplayName,DisplayVersion,Publisher,InstallDate |格式表-AutoSize

我一直在寻找stackoverflow中的其他问题,但没有一个给我一个解决方案 . 我需要搜索每个磁盘单元(而不仅仅是C :) . 有人可以告诉我一个可能的解决方案吗?

谢谢 .

1 回答

  • 1

    您无法直接运行PowerShell命令,您必须通过PowerShell进程启动它们:

    powershell -command "PowerShell commands with parameters"
    

    所以改变你的exec调用如下:

    Process p = Runtime.getRuntime().exec("powershell -command \"Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize\"");
    

相关问题