如何在Java中获取System变量值?

时间:2022-06-28 11:20:29

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

如何获取存在的系统变量值

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

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

如果我为我创建的系统变量尝试相同的值,它显示空值

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

8 个解决方案

#1


101  

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

使用System.getenv(String)方法,将变量的名称传递给read。

#2


39  

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.

您应该看到环境变量的值。如果没有,您可能需要重新打开shell(DOS)或注销并重新登录。

#3


21  

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)是在返回时引入的,然后弃用。弃用(愚蠢地,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=value command line arguments. For example, the values for the properties maxInMemory and pagingDirectory are set in the command below:

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

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

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

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

#4


7  

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. 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 CustomCheckedException("ENV_VAR1 is not set in the environment"));

#5


5  

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.

但是,我不确定系统变量与环境变量的区别,所以如果你能澄清我可以提供更多帮助。

#6


1  

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在某种缓存中保留了它的环境变量,重启是一种刷新它的方法。我不确定但可能有不同的方法,但如果你不太经常改变你的变量值,这可能就足够了。

#7


1  

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.

如上面的sombody所述,重新启动eclipse对我来说是用户定义的环境变量。在我重新启动eclipse IDE之后,System.getenv()正在拾取我的环境变量。

#8


1  

From comment @Yasin:

来自评论@Yasin:

The restart option from eclipse doesn't work. Just closing the application, and starting it again will solve the issue.

eclipse的重启选项不起作用。只需关闭应用程序,然后重新启动即可解决问题。

#1


101  

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

使用System.getenv(String)方法,将变量的名称传递给read。

#2


39  

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.

您应该看到环境变量的值。如果没有,您可能需要重新打开shell(DOS)或注销并重新登录。

#3


21  

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)是在返回时引入的,然后弃用。弃用(愚蠢地,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=value command line arguments. For example, the values for the properties maxInMemory and pagingDirectory are set in the command below:

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

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

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

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

#4


7  

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. 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 CustomCheckedException("ENV_VAR1 is not set in the environment"));

#5


5  

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.

但是,我不确定系统变量与环境变量的区别,所以如果你能澄清我可以提供更多帮助。

#6


1  

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在某种缓存中保留了它的环境变量,重启是一种刷新它的方法。我不确定但可能有不同的方法,但如果你不太经常改变你的变量值,这可能就足够了。

#7


1  

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.

如上面的sombody所述,重新启动eclipse对我来说是用户定义的环境变量。在我重新启动eclipse IDE之后,System.getenv()正在拾取我的环境变量。

#8


1  

From comment @Yasin:

来自评论@Yasin:

The restart option from eclipse doesn't work. Just closing the application, and starting it again will solve the issue.

eclipse的重启选项不起作用。只需关闭应用程序,然后重新启动即可解决问题。