如何在Java中获取JAVA_HOME和CATALINA_HOME环境变量?
How to get JAVA_HOME and CATALINA_HOME environment variables in Java?
我需要在 Java 中获取 JAVA_HOME 和 CATALINA_HOME 环境变量的值。我是 运行 一个 JAR(不是 WAR),在 System.getProperties()
中我只能找到 "java.home"
。
我在这里阅读了一些关于它的其他问题,尝试了它们,但无法使它们工作 - 我得到了不同的异常或结果只是 null
值。
我记得很多年前用JNA做过类似的事情,但那是在Windows的16位时代。我能找到的最新 JNA jar 是从 2011 年开始的,运行 它在 intelliJ 中有效,但是当我在 maven 中构建时,我得到关于找不到一些 classes.
我将继续研究 JNA 方向,很高兴收到任何帮助 and/or 想法。我认为我只是缺少正确的 Maven 依赖项.
这是我 class 到目前为止的源代码:
// C:\devtools\java-external-jars\jna-4.2.2\jna-platform-4.2.2.jar
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() { }
/**
*
* {@code String stringValue = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName");}
*/
public static String registryGetStringValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read a string
String stringValue = Advapi32Util.registryGetStringValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %s\n", stringValue);
return stringValue;
}
/**
*
* {@code int timeout = Advapi32Util.registryGetIntValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows", "ShutdownWarningDialogTimeout");}
*/
public static int registryGetIntValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read an int (& 0xFFFFFFFFL for large unsigned int)
int intValue = Advapi32Util.registryGetIntValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", intValue, intValue & 0xFFFFFFFFL);
return intValue;
}
/**
*
* {@code int timeout = Advapi32Util.registryGetLongValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows", "ShutdownWarningDialogTimeout");}
*/
public static long registryGetLongValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read an int (& 0xFFFFFFFFL for large unsigned int)
long longValue = Advapi32Util.registryGetLongValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", longValue, longValue & 0xFFFFFFFFL);
return longValue;
}
/**
*
* {@code Advapi32Util.registryCreateKeyPath(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug");}
*/
public static void registryCreateKeyPath(WinReg.HKEY root, String keyPath) {
// Create a key and write a string
Advapi32Util.registryCreateKey(root, keyPath);
}
/**
* {@code Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug", "url", "");}
*/
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);
}
/**
*
* {@code Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug");}
*/
public static void registryDeleteKey(WinReg.HKEY root, String keyPath) {
// Delete a key
Advapi32Util.registryDeleteKey(root, keyPath);
}
}
有一个System.getenv
方法可以帮助获取指定环境变量的值:
System.getenv("JAVA_HOME");
System.getenv("CATALINA_HOME");
如果安全管理器允许访问环境变量。否则,将抛出 SecurityException
。
Tomcat主目录或Catalina目录存放在Java系统属性环境中。如果Java web 应用程序部署到Tomcat web 服务器,我们可以使用以下命令
获取Tomcat 目录
System.out.println(System.getProperty("catalina.base"));
System.out.println(System.getProperty("catalina.home"));
//you can store it in string
String caltelinaHome =System.getProperty("catalina.home");
我需要在 Java 中获取 JAVA_HOME 和 CATALINA_HOME 环境变量的值。我是 运行 一个 JAR(不是 WAR),在 System.getProperties()
中我只能找到 "java.home"
。
我在这里阅读了一些关于它的其他问题,尝试了它们,但无法使它们工作 - 我得到了不同的异常或结果只是 null
值。
我记得很多年前用JNA做过类似的事情,但那是在Windows的16位时代。我能找到的最新 JNA jar 是从 2011 年开始的,运行 它在 intelliJ 中有效,但是当我在 maven 中构建时,我得到关于找不到一些 classes.
我将继续研究 JNA 方向,很高兴收到任何帮助 and/or 想法。我认为我只是缺少正确的 Maven 依赖项.
这是我 class 到目前为止的源代码:
// C:\devtools\java-external-jars\jna-4.2.2\jna-platform-4.2.2.jar
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() { }
/**
*
* {@code String stringValue = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName");}
*/
public static String registryGetStringValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read a string
String stringValue = Advapi32Util.registryGetStringValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %s\n", stringValue);
return stringValue;
}
/**
*
* {@code int timeout = Advapi32Util.registryGetIntValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows", "ShutdownWarningDialogTimeout");}
*/
public static int registryGetIntValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read an int (& 0xFFFFFFFFL for large unsigned int)
int intValue = Advapi32Util.registryGetIntValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", intValue, intValue & 0xFFFFFFFFL);
return intValue;
}
/**
*
* {@code int timeout = Advapi32Util.registryGetLongValue(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows", "ShutdownWarningDialogTimeout");}
*/
public static long registryGetLongValue(WinReg.HKEY root, String keyPath, String keyName) {
// Read an int (& 0xFFFFFFFFL for large unsigned int)
long longValue = Advapi32Util.registryGetLongValue(root, keyPath, keyName);
System.out.printf(keyName + " value is: %d (%d as unsigned long)\n", longValue, longValue & 0xFFFFFFFFL);
return longValue;
}
/**
*
* {@code Advapi32Util.registryCreateKeyPath(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug");}
*/
public static void registryCreateKeyPath(WinReg.HKEY root, String keyPath) {
// Create a key and write a string
Advapi32Util.registryCreateKey(root, keyPath);
}
/**
* {@code Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug", "url", "");}
*/
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);
}
/**
*
* {@code Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "SOFTWARE\Whosebug");}
*/
public static void registryDeleteKey(WinReg.HKEY root, String keyPath) {
// Delete a key
Advapi32Util.registryDeleteKey(root, keyPath);
}
}
有一个System.getenv
方法可以帮助获取指定环境变量的值:
System.getenv("JAVA_HOME");
System.getenv("CATALINA_HOME");
如果安全管理器允许访问环境变量。否则,将抛出 SecurityException
。
Tomcat主目录或Catalina目录存放在Java系统属性环境中。如果Java web 应用程序部署到Tomcat web 服务器,我们可以使用以下命令
获取Tomcat 目录System.out.println(System.getProperty("catalina.base"));
System.out.println(System.getProperty("catalina.home"));
//you can store it in string
String caltelinaHome =System.getProperty("catalina.home");