我们可以在 Java 中读取操作系统环境变量吗?

2023-07-14Java开发问题
0

本文介绍了我们可以在 Java 中读取操作系统环境变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的操作系统是 windows7.我想在我的 Java 应用程序中读取环境变量.我搜索了谷歌,很多人的答案是使用方法 System.getProperty(String name)System.getenv(String name).但这似乎不起作用.通过该方法,我可以读取在 JVM 中定义的一些变量的值.

My OS is windows7. I want to read the environment variables in my Java application. I have searched google and many people's answer is to use the method System.getProperty(String name) or System.getenv(String name). But it doesn't seem to work. Through the method, I can read some variable's value that defined in the JVM.

如果我设置了一个名为Config"的环境变量,其值为some config information",如何在Java中获取该值?

If I set an environment variable named "Config", with value "some config information", how can I get the value in Java?

推荐答案

你应该使用 System.getenv(),例如:

You should use System.getenv(), for example:

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

从 IDE 运行时,您可以定义其他环境变量,该变量将传递给您的 Java 应用程序.例如,在 IntelliJ IDEA 中,您可以在 run 的环境变量"字段中添加环境变量配置.

When running from an IDE you can define additional environment variable which will be passed to your Java application. For example in IntelliJ IDEA you can add environment variables in the "Environment variables" field of the run configuration.

请注意(正如@vikingsteve 的评论中所提到的)JVM 与任何其他 Windows 可执行文件一样,对环境变量的系统级更改仅在重新启动时传播到进程.

有关更多信息,请查看 Java 教程的环境变量"部分.

System.getProperty(String name) 用于获取 Java 系统属性 不是环境变量.

Notice (as mentioned in the comment by @vikingsteve) that the JVM, like any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.

For more information take a look at the "Environment Variables" section of the Java tutorial.

System.getProperty(String name) is intended for getting Java system properties which are not environment variables.

这篇关于我们可以在 Java 中读取操作系统环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13