2016-11-30 108 views
4

我需要在Java中獲取JAVA_HOME和CATALINA_HOME環境變量的值。我正在運行一個JAR(不是WAR),並且在System.getProperties()中我只能找到"java.home"如何在Java中獲得JAVA_HOME和CATALINA_HOME環境變量?

我在這裏閱讀了一些關於它的其他問題,嘗試了它們,但無法讓它們工作 - 我得到了不同的例外或僅僅因爲值。

我記得許多年前使用JNA做了類似的事情,但它在Windows的16位時代。最新的JNA JAR我能找到的是從2011年開始,在intelliJ的作品中運行它,但是當我在maven中構建時,我會遇到關於找不到某些類的錯誤。

我會繼續調查JNA的方向,並很樂意接受任何幫助和/或想法。我認爲我只錯過了Maven依賴關係

這是我的課的源代碼至今:

// 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\\StackOverflow");} 
    */ 
    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\\StackOverflow", "url", "http://stackoverflow.com/a/6287763/277307");} 
    */ 
    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\\StackOverflow");} 
    */ 
    public static void registryDeleteKey(WinReg.HKEY root, String keyPath) { 
     // Delete a key 
     Advapi32Util.registryDeleteKey(root, keyPath); 
    } 
} 

回答

3

有一個System.getenv方法,有助於optain指定的環境變量的值:

System.getenv("JAVA_HOME"); 
System.getenv("CATALINA_HOME"); 

如果安全管理器允許訪問環境變量。否則,將拋出一個SecurityException

+1

找到___JNA___ ** Maven的依賴**的作品:'<! - https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform - > net.java .dev.jna JNA平臺 4.0.0 ' –

相關問題