简体   繁体   English

如何在 Java 中获取系统变量值?

[英]How can I get System variable value in Java?

How can I get the System Variable value which is present in如何获取系统变量值存在于

MyComputer -> Properties -> Advanced -> Environment Variables -> System Variables

in Java?在 Java 中?

Edit编辑

I have used System.getenv() method.我使用了System.getenv()方法。

It is printing value if I give如果我给,它是印刷价值

System.out.println(System.getenv("JAVA_HOME"));

and it is showing null value if I try the same for system variable created by me如果我对我创建的系统变量尝试相同的值,它会显示null

System.out.println(System.getenv("DBE"));

使用System.getenv(String)方法,传递要读取的变量名称。

To clarify, system variables are the same as environment variables. 为了明确起见,系统变量与环境变量相同。 User environment variables are set per user and are different whenever a different user logs in. System wide environment variables are the same no matter what user logs on. 用户环境变量是为每个用户设置的,每当一个不同的用户登录时,该用户环境变量就会不同。无论用户登录什么,系统范围的环境变量都是相同的。

To access either the current value of a system wide variable or a user variable in Java, see below: 要访问Java中系统范围变量或用户变量的当前值,请参见以下内容:

String javaHome = System.getenv("JAVA_HOME");

For more information on environment variables see this wikipedia page . 有关环境变量的更多信息,请参见此Wikipedia页面

Also make sure the environment variable you are trying to read is properly set before invoking Java by doing a: 通过执行以下操作,还要确保在调用Java之前正确设置了要尝试读取的环境变量:

echo %MYENVVAR%

You should see the value of the environment variable. 您应该看到环境变量的值。 If not, you may need to reopen the shell (DOS) or log off and log back on. 如果不是,则可能需要重新打开外壳(DOS)或注销并重新登录。

There are a few details of interest when getting system/environment properties. 获取系统/环境属性时,有一些有趣的细节。

First, System.getenv(String) was introduced way-back-when, then deprecated. 首先,将System.getenv(String)反向引入,然后弃用。 The deprecation (foolishly, IHMO) continued all the way into JSE 1.4 . 弃用(愚蠢的是IHMO)一直持续到JSE 1.4

It got re-introduced in JSE 5 . 在JSE 5中重新引入

Those are set using the Environment Variables panel in Windows. 这些是使用Windows中的“环境变量”面板设置的。 Changes to the variables may not get picked up until your current VM is shutdown, and the CMD.exe instance is exited. 在关闭当前VM并退出CMD.exe实例之前,可能不会获取对变量的更改。

In contrast to the environment properties, Java also has Java system properties, accessible through System.getProperties() . 与环境属性相反,Java还具有Java系统属性,可通过System.getProperties()访问。 These variables can be initialized when the VM is started using a series -D name = value command line arguments. 在启动VM时,可以使用-D name = value命令行参数系列来初始化这些变量。 For example, the values for the properties maxInMemory and pagingDirectory are set in the command below: 例如,在以下命令中设置属性maxInMemorypagingDirectory的值:

C:\> java.exe -DmaxInMemory=100M -DpagingDirectory=c:\temp -jar myApp.jar

These properties can be modified at runtime, barring security policy restrictions. 可以在运行时修改这些属性,除非存在安全策略限制。

Actually the variable can be set or not, so, In Java 8 or superior its nullable value should be wrapped into an Optional object, which allows really good features. 实际上,该变量可以设置或不设置,因此,在Java 8或更高版本中,应将其可为空的值包装到Optional对象中,该对象具有非常好的功能。 In the following example we gonna try to obtain the variable ENV_VAR1 , if it doesnt exist we may throw some custom Exception to alert about it: 在下面的示例中,我们将尝试获取变量ENV_VAR1 ,如果它不存在,我们可能会抛出一些自定义Exception来警告它:

String ENV_VAR1 = Optional.ofNullable(System.getenv("ENV_VAR1")).orElseThrow(
  () -> new CustomException("ENV_VAR1 is not set in the environment"));

Google says to check out getenv() : Google说要检出getenv()

Returns an unmodifiable string map view of the current system environment. 返回当前系统环境的不可修改的字符串映射视图。

I'm not sure how system variables differ from environment variables, however, so if you could clarify I could help out more. 我不确定系统变量与环境变量有何不同,因此,如果您能澄清一点,我可以提供更多帮助。

As mentioned by sombody above, restarting eclipse worked for me for the user defined environment variable. 如上面的sombody所述,对于用户定义的环境变量,重新启动eclipse对我有用。 After I restart eclipse IDE, System.getenv() is picking up my environment variable. 重新启动eclipse IDE之后, System.getenv()正在获取我的环境变量。

Have you tried rebooting since you set the environment variable? 自设置环境变量以来,您是否尝试过重启?

It appears that Windows keeps it's environment variable in some sort of cache, and rebooting is one method to refresh it. Windows似乎将其环境变量保留在某种缓存中,重新启动是刷新它的一种方法。 I'm not sure but there may be a different method, but if you are not going to be changing your variable value too often this may be good enough. 我不确定,但是可能有不同的方法,但是如果您不想太频繁地更改变量值,这可能就足够了。

Are you on a linux system? 您在Linux系统上吗? If so be sure you are exporting your variable. 如果是这样,请确保您正在导出变量。

myVar=testvalue; export myVar

I get null unless I use export to define the value globally. 除非使用export全局定义值,否则我将为null。

This code gives you a map of Environment variables' names and the corresponding values.此代码为您提供环境变量名称和相应值的 map。

public static void main(String[] args) {
            Map<String, String> map = System.getenv();
            System.out.println(map);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM