4

I can go to regedit in windows then go to edit->find and type in the key I want to find in my computer (in my case Maxima) and locate the required key (of maxima) (in my case it is in "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Maxima-5.17.1_is1") but I need to do this dynamically in java and find the current version of maxima installed and its directory location in windows. I have no idea how to proceed.

I tried to use the methods stated here: read/write to Windows Registry using Java, but to use these methods I need to know the key. How can I find the key dynamically in java? Or is there any other ways to find the version and directory location of a software in windows using java?

Community
  • 1
  • 1
Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
  • possible duplicate of [read/write to Windows Registry using Java](http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java) – Erwin Bolwidt Jul 05 '15 at 05:04
  • not duplicate. I tried the methods mentioned there but "to use those methods I need to know the key" – Riyafa Abdul Hameed Jul 05 '15 at 05:28
  • Use `hKey=WinRegistry.HKEY_LOCAL_MACHINE` and `key="SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\Uninstall\\Maxima-5.17.1_is1"`. The registry key is a simple string containing everything after the `HKEY_xxx`. So there should be no problem setting up the key dynamically containing some version information of maxima. – Lukas Thomsen Jul 05 '15 at 05:35
  • How can I find the key dynamically without hardcoding it? I mean in this case I had to go to RegEdit and search for maxima to find its location. I want to do this in java without manually going to RegEdit – Riyafa Abdul Hameed Jul 05 '15 at 05:40

2 Answers2

4

I will use this class for your answer. Because it is written in pure java code.

  1. Have a WinRegistry class from here.
  2. Get a list of all keys in parent key.
  3. filter list to get the most appropriate key (Or exact key).
  4. Then you can check the value you want in this key.

Here is the code to help you :

List<String> ls = WinRegistry.readStringSubKeys(WinRegistry.HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
String key = ls.stream().filter(st -> st.matches("Maxima.*")).findAny().get();

Now this key value will be Maxima-5.17.1_is1 (if present otherwise java.util.NoSuchElementException will be thrown). And you can use it to get any Value.

Community
  • 1
  • 1
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • Bear in mind that accessing private methods is likely to break eventually. In an object-oriented API (like Java SE), public methods never change, but private methods are an implementation detail that can change in any future release. – VGR Jul 05 '15 at 23:41
4

I would avoid forcing access to private methods, because:

  1. They may not be there in a future release of Java. Literally, the next minor update may not have those methods.
  2. Code is less portable if it can only work in the absence of a SecurityManager.

If you use reg.exe, your code is guaranteed to work in all versions of Java, at least for as long as Microsoft includes reg.exe with Windows:

ProcessBuilder builder = new ProcessBuilder("reg", "query",
    "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
Process reg = builder.start();
try (BufferedReader output = new BufferedReader(
    new InputStreamReader(reg.getInputStream()))) {

    Stream<String> keys = output.lines().filter(l -> !l.isEmpty());
    Stream<String> matches = keys.filter(l -> l.contains("\\Maxima"));
    Optional<String> key = matches.findFirst();
    // Use key ... 
}
reg.waitFor();
VGR
  • 40,506
  • 4
  • 48
  • 63