在 Java 1.4 中访问 Windows 系统变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048661/
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-09-15 12:43:00  来源:igfitidea点击:

Accessing Windows system variables in Java 1.4

javawindowsenvironment-variablesjava1.4

提问by Saravanan M

What is the best/foolproof way to get the values of environment variables in Windows when using J2SE 1.4 ?

使用 J2SE 1.4 时在 Windows 中获取环境变量值的最佳/万无一失的方法是什么?

采纳答案by Michael Zilbermann

You have definitely no way to access environment variables straigthly from java API. The only way to achieve that with Runtime.exec with such a code :

您绝对无法直接从 Java API 访问环境变量。使用 Runtime.exec 使用这样的代码实现这一目标的唯一方法:

Process p = null;
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
// System.out.println(OS);
if (OS.indexOf("windows 9") > -1) {
  p = r.exec( "command.com /c set" );
}
else if ( (OS.indexOf("nt") > -1)
       || (OS.indexOf("windows 2000") > -1 )
       || (OS.indexOf("windows xp") > -1) ) {
  // thanks to JuanFran for the xp fix!
  p = r.exec( "cmd.exe /c set" );
}

Although you can access Java variables thanks to System.getProperties(); But you would only get some env variables mapped by JVM itself, and additional data you could provide on java command line with "-Dkey=value"

尽管您可以通过 System.getProperties() 访问 Java 变量;但是你只会得到一些由 JVM 本身映射的环境变量,以及你可以在 java 命令行上使用“-Dkey=value”提供的额外数据

For more information see http://www.rgagnon.com/javadetails/java-0150.html

有关更多信息,请参阅http://www.rgagnon.com/javadetails/java-0150.html

回答by Eric Petroelje

You can use getEnv() to get environment variables:

您可以使用 getEnv() 获取环境变量:

String variable = System.getenv("WINDIR");  
System.out.println(variable);  

I believe the getEnv() function was deprecated at some point but then "undeprecated" later in java 1.5

我相信 getEnv() 函数在某些时候已被弃用,但后来在 java 1.5 中“不推荐使用”

ETA:

预计到达时间:

I see the question now refers specifically to java 1.4, so this wouldn't work for you (or at least you might end up with a deprecation warning). I'll leave the answer here though in case someone else stumbles across this question and is using a later version.

我现在看到这个问题专门针对 java 1.4,所以这对你不起作用(或者至少你最终可能会收到弃用警告)。我会在这里留下答案,以防其他人偶然发现这个问题并使用更高版本。

回答by Jeremy Smyth

There's a switch to the JVM for this. From here:

为此,可以切换到 JVM。从这里

Start the JVM with the "-D" switch to pass properties to the application and read them with the System.getProperty() method.

使用“-D”开关启动 JVM 以将属性传递给应用程序并使用 System.getProperty() 方法读取它们。

SET myvar=Hello world
SET myothervar=nothing
java -Dmyvar="%myvar%" -Dmyothervar="%myothervar%" myClass

then in myClass

然后在我的课堂

String myvar = System.getProperty("myvar");
String myothervar = System.getProperty("myothervar");

回答by Nick Holt

Pass them into the JVM as -D system properties, for example:

将它们作为 -D 系统属性传递到 JVM,例如:

java -D<java var>=%<environment var>%

That way you don't become tied to a particular OS.

这样您就不会被绑定到特定的操作系统。

回答by b1nary.atr0phy

Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
     System.out.format("%s=%s%n", envName, env.get(envName));
}

回答by jhunovis

Apache Commons Exec provides with "org.apache.commons.exec.environment.EnvironmentUtils" a way to get the environment variables on JDK's prior 1.5:

Apache Commons Exec 通过“org.apache.commons.exec.environment.EnvironmentUtils”提供了一种获取 JDK 1.5 之前的环境变量的方法:

(String) EnvironmentUtils.getProcEnvironment().get("SOME_ENV_VAR")