开发者

Get MS-Windows Installed Applications from Java

开发者 https://www.devze.com 2023-01-24 23:41 出处:网络
Is it possible to get a list of installed appl开发者_运维知识库ications (like the list from the un-install programs) from a windows vista computer with java?If you mean installed applications I don\'t

Is it possible to get a list of installed appl开发者_运维知识库ications (like the list from the un-install programs) from a windows vista computer with java?


If you mean installed applications I don't think it's possible by directly using Java SDK (also because it's not a cross platform requirement).. what I think you can do is to use an external native API to interact with windows registry (like jRegistryKey) and retrieve the information you need..

Instead if you want to get all the running applications from a Java program you could parse tasklist.exe output as explained here.


Not a Solution but a workaround!!

Getting windows native information using java SDK is not possible without support of external APIs. Instead of using external APIs (which is mostly LGPL licensed and not completely open), we can use the shell commands to get the same.

For getting the installed softwares list, use ProcessBuilder or Runtime.exec to run one of the following PowerShell commands:

  1. Get-WmiObject -class Win32_Product | Select-Object -Property Name - This is bit slower! It uses Win32_Product class.
  2. Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate - This is faster and can give additional details. This uses PS registry provider.

You can stream the output of these and process it.

This is just a workaround and it is as per my analysis. As java is completely platform independent, fetching native information becomes difficult and the use of platform native tools (like command shell,, power shell etc.,) is a must.


package Vishal;

import com.sun.jna.platform.win32.Advapi32Util;
import static com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;
import java.util.ArrayList;
import java.util.TreeMap;

public class GenerateInstalledApplicationList 
{
    ArrayList<String> getlist()
    {
        ArrayList<String> arr = new ArrayList();
        String [] keys = Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
    String temp;
    for (String key : keys) 
    {
        temp = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall" + "\\" +key;
        TreeMap<String, Object> tr = Advapi32Util.registryGetValues(HKEY_LOCAL_MACHINE,temp); 
        if(tr.isEmpty())
        {
            if(!key.contains("Update"))//all the instances of update are not actually installed applications
            {
            arr.add(key);
            }
        }
        else
        {
            if(tr.containsKey("DisplayName"))
            {
                String str = (String) tr.get("DisplayName");
                if(!str.contains("Update"))
                {
                arr.add(str);
                }
            }
        }     
    }
    return arr;
   }}

Just Copy and paste this code it will return all the installed applications only thing you will need is the jna api.

0

精彩评论

暂无评论...
验证码 换一张
取 消