I need to get the values โโof the environment variable JAVA_HOME and CATALINA_HOME in Java. I run JAR (not WAR), and in System.getProperties()I can only find "java.home".
I read some other questions about this here, tried them, but could not get them to work - as a result, I got different exceptions or just a value null.
I remember that I did something similar many years ago using JNA, but that was in the 16-bit era of Windows. The last JNA jar I can find is from 2011, running it in IntelliJ, but when I build in maven, I get errors in that I cannot find some classes.
I will continue to explore the direction of JNA and will be happy to receive any help and / or ideas. I think that I am missing only dependence on Maven .
This is the source code for my class:
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
public class WinRegistryHelper {
public static final String SYSTEM_ENVIRONMENT_KEY_PATH = "SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment";
public static final int HKEY_CURRENT_USER = 0x80000001;
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
public static final int REG_SUCCESS = 0;
public static final int REG_NOTFOUND = 2;
public static final int REG_ACCESSDENIED = 5;
private static final int KEY_ALL_ACCESS = 0xf003f;
private static final int KEY_READ = 0x20019;
private static final Preferences userRoot = Preferences.userRoot();
private static final Preferences systemRoot = Preferences.systemRoot();
private static final Class<? extends Preferences> userClass = userRoot.getClass();
private static final Method regOpenKey;
private static final Method regCloseKey;
private static final Method regQueryValueEx;
private static final Method regEnumValue;
private static final Method regQueryInfoKey;
private static final Method regEnumKeyEx;
private static final Method regCreateKeyEx;
private static final Method regSetValueEx;
private static final Method regDeleteKey;
private static final Method regDeleteValue;
private WinRegistryHelper() { }
public static String registryGetStringValue(WinReg.HKEY root, String keyPath, String keyName) {
String stringValue = Advapi32Util.registryGetStringValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %s\n", stringValue);
return stringValue;
}
public static int registryGetIntValue(WinReg.HKEY root, String keyPath, String keyName) {
int intValue = Advapi32Util.registryGetIntValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", intValue, intValue & 0xFFFFFFFFL);
return intValue;
}
public static long registryGetLongValue(WinReg.HKEY root, String keyPath, String keyName) {
long longValue = Advapi32Util.registryGetLongValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", longValue, longValue & 0xFFFFFFFFL);
return longValue;
}
public static void registryCreateKeyPath(WinReg.HKEY root, String keyPath) {
Advapi32Util.registryCreateKey(root, keyPath);
}
public static void registrySetStringValue(WinReg.HKEY root, String keyPath, String keyName, String keyValue) {
Advapi32Util.registrySetStringValue(root, keyPath, keyName, keyValue);
}
public static void registrySetIntValue(WinReg.HKEY root, String keyPath, String keyName, int keyValue) {
Advapi32Util.registrySetIntValue(root, keyPath, keyName, keyValue);
}
public static void registrySetLongValue(WinReg.HKEY root, String keyPath, String keyName, long keyValue) {
Advapi32Util.registrySetLongValue(root, keyPath, keyName, keyValue);
}
public static void registryDeleteKey(WinReg.HKEY root, String keyPath) {
Advapi32Util.registryDeleteKey(root, keyPath);
}
}