javawindowswinapiregistrywinreg

How to get the list of installed applications on Windows 10 from Java


I'm trying to get a list of all applications installed in windows 10 from my Java program. I have tried the following:

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

and I get:

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

I tried too:

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

With similar result.

Finally, I tried to use the library "win32" but it only returns the name of some of the programs installed.

I need the same result that I get when execute in powershell the following command:

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

I had been looking for other questions in stackoverflow but none of them give me a solution. I need to search in every disk unit (and not only in C:). Someone can tell me a possible solution?

Thanks.


Solution

  • You can't run PowerShell commands directly, you have to launch them through the PowerShell process:

    powershell -command "PowerShell commands with parameters"
    

    So change your exec call like this:

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