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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/531694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:54:22  来源:igfitidea点击:

How can I get System variable value in Java?

javaenvironment-variables

提问by raja

How can I get the System Variable value which is present in

我怎样才能获得存在于中的系统变量值

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

in 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 nullvalue if I try the same for system variable created by me

null如果我对我创建的系统变量尝试相同的方法,它就会显示价值

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

回答by Rob

Use the System.getenv(String)method, passing the name of the variable to read.

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

回答by Chris Bunch

Google says to check out getenv():

谷歌说要检查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.

但是,我不确定系统变量与环境变量有何不同,因此,如果您能澄清一下,我可以提供更多帮助。

回答by Elijah

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.

有关环境变量的更多信息,请参阅此维基百科页面

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) 或注销并重新登录。

回答by trilobite

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. 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.

Windows 似乎将它的环境变量保存在某种缓存中,重新启动是刷新它的一种方法。我不确定,但可能有不同的方法,但如果您不打算过于频繁地更改变量值,这可能就足够了。

回答by Dilum Ranatunga

There are a few details of interest when getting system/environment properties.

获取系统/环境属性时,有一些有趣的细节。

First, System.getenv(String)was introduced way-back-when, then deprecated. The deprecation (foolishly, IHMO) continued all the way into JSE 1.4.

首先,System.getenv(String)被引入 way-back-when,然后被弃用。弃用(愚蠢的是,IHMO)一直持续到 JSE 1.4

It got re-introduced in JSE 5.

在 JSE 5 中重新引入

Those are set using the Environment Variables panel in Windows. Changes to the variables may not get picked up until your current VM is shutdown, and the CMD.exe instance is exited.

这些是使用 Windows 中的环境变量面板设置的。在关闭当前 VM 并退出 CMD.exe 实例之前,可能不会获取对变量的更改。

In contrast to the environment properties, Java also has Java system properties, accessible through System.getProperties(). These variables can be initialized when the VM is started using a series -Dname=valuecommand line arguments. For example, the values for the properties maxInMemoryand pagingDirectoryare set in the command below:

与环境属性相反,Java 还具有 Java 系统属性,可通过System.getProperties(). 当使用一系列命令行参数启动 VM 时,可以初始化这些变量。例如,属性的值和在下面的命令中设置:-Dname=valuemaxInMemorypagingDirectory

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

These properties can be modified at runtime, barring security policy restrictions.

这些属性可以在运行时修改,除非安全策略限制。

回答by EliuX

Actually the variable can be set or not, so, In Java 8 or superior its nullable value should be wrapped into an Optionalobject, which allows really good features. 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:

实际上变量可以设置也可以不设置,因此,在 Java 8 或更高版本中,它的可空值应该被包装到一个Optional对象中,这允许真正好的特性。在下面的例子中,我们将尝试获取变量ENV_VAR1,如果它不存在,我们可能会抛出一些自定义异常来提醒它:

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

回答by voidMainReturn

As mentioned by sombody above, restarting eclipse worked for me for the user defined environment variable. After I restart eclipse IDE, System.getenv()is picking up my environment variable.

正如上面的某人所提到的,重新启动 eclipse 对我来说对用户定义的环境变量有用。在我重新启动 eclipse IDE 后,System.getenv()正在获取我的环境变量。

回答by keving

Are you on a linux system? If so be sure you are exporting your variable.

你是linux系统吗?如果是这样,请确保您正在导出变量。

myVar=testvalue; export myVar

I get null unless I use export to define the value globally.

除非我使用导出来全局定义值,否则我会得到 null。