5

How can one get a list of installed software and its version in Java?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Spaniard89
  • 2,359
  • 4
  • 34
  • 55

3 Answers3

0

There's another question on StackOverflow that checks for the existence of a particular software.

You can make a list of all filepaths that have

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall

in the beginning, then take whatever is at the end of this filepath to be the name of the software.

Community
  • 1
  • 1
Antimony
  • 2,230
  • 3
  • 28
  • 38
  • but isn't it OS dependent as, i can only make it work with Windows. But what if i want it to work with Linux as well? Is it possible to parse the file location or search for the name of software in a particular directory? Any help would be very much appreciated. – Spaniard89 Jul 18 '12 at 11:01
  • For Linux: In order to run Linux shell commands from Java, see [example](http://linuxprogram.wordpress.com/2006/10/22/how-to-excute-a-linux-shell-command-in-java/) As for what to run: [see this](http://linuxprogram.wordpress.com/2006/10/22/how-to-excute-a-linux-shell-command-in-java/) – Antimony Jul 18 '12 at 11:12
  • 1
    i am extremely sorry but how would this post, solve my problem? – Spaniard89 Jul 18 '12 at 11:14
  • Alright. Going step by step. You first write a program that runs the Linux shell command (which gives a list of software/apps installed in the Linux machine) for you, then save the result in a list. This list is what you needed. – Antimony Jul 18 '12 at 11:16
  • Thank you very much @Antimony but which linux shell command gives the list of applications installed? – Spaniard89 Jul 18 '12 at 11:21
  • Dude. Read my second comment properly, it's got 2 links. You might have missed the second one. – Antimony Jul 18 '12 at 11:24
  • But aren't they same? Both the link redirect to the same web page! Thank you mate! – Spaniard89 Jul 18 '12 at 11:26
  • @Spaniard89 For Linux distributions, [see this](https://unix.stackexchange.com/questions/20979/how-do-i-list-all-installed-programs). – MC Emperor Mar 17 '19 at 12:55
0

I think following code might help you. I am new to java so pls edit my code if any changes.

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class getInstalledAppList {

  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  static String s = REGQUERY_UTIL + "HKEY_LOCAL_MACHINE\\Software"
                    + "\\Microsoft\\Windows\\CurrentVersion\\Uninstall";





public static String getCurrentUserPersonalFolderPath() {
    try {
      Process process = Runtime.getRuntime().exec(s);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();

      String result = reader.getResult();
      int p = result.indexOf(REGSTR_TOKEN);

      if (p == -1)
         return null;

      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (IOException | InterruptedException e) {
      return null;
    }
  }



  static class StreamReader extends Thread {
    private final InputStream is;
    private final StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    @Override
    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { e.printStackTrace(); }
      }

    String getResult() {
      return sw.toString();
    }
  }

  public static void main(String s[]) {

      getDisplayNameDword( getCurrentUserPersonalFolderPath()  );
  }

  private static void getDisplayNameDword(String str){

      Set<String> set = new HashSet<>();

      String [] array = new String[500];


      array = str.split("\n");

      for(String i : array){

          set.add( getName(i.trim()) ); 

      }


      Iterator i = set.iterator();

      while(i.hasNext()){

          System.out.println( i.next() );

      }

  }

  private static String getName(String s){
  Process process = null;
  try {
            // Run reg query, then read output with StreamReader (internal class)
             process = Runtime.getRuntime().exec("reg query " + 
                    '"'+ s + "\" /v " + "DisplayName");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();


            // Parse out the value
            String[] parsed = reader.getResult().split(REGSTR_TOKEN);
            if (parsed.length > 1) {
            return  (parsed[parsed.length-1]).trim();
            }

       } catch (IOException | InterruptedException e) {
           e.printStackTrace();
       }
  return null;
  }
}

or use psinfo.exe to get installed app list in windows.

Kushal
  • 8,100
  • 9
  • 63
  • 82
Arun
  • 15
  • 1
  • 7
0

I wrote a little library (JavaListApps) that does this based on ListPrograms which is in turn based on a VB Script linked from a StackOverflow Post

My need is a little limited, so there are unimplemented parts compared to the C++ version.

drone.ah
  • 1,135
  • 14
  • 28