Java 以独立于平台的方式将环境变量传递给 JVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462069/
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
Passing environment variables to a JVM, in a platform-independent manner
提问by Matt Ball
I'm developing a J2EE application that runs in JBoss on a Windows Vista machine, but the application will end up on a Linux machine. Is there a way to pass in the value of an environment variable in a platform independent way?
我正在开发一个 J2EE 应用程序,它在 Windows Vista 机器上的 JBoss 中运行,但该应用程序最终会在 Linux 机器上运行。有没有办法以独立于平台的方式传入环境变量的值?
I think (but I'm not sure) the platform-sensitive way would be:
我认为(但我不确定)平台敏感的方式是:
-Denv_var=%MY_ENV_VAR% (Windows)
-Denv_var=$MY_ENV_VAR (Linux)
and from there I would access the value (in Java) using
从那里我将使用(在 Java 中)访问值
System.getProperty("MY_ENV_VAR");
— is that correct?
- 那是对的吗?
The Javadoc for System.getenv(String name)
seem to imply that method is platform-dependent, but I'm not clear on that. Could I just skip passing the variable into the JVM completely, and use getenv()
after using setting the value for an environment variable using the OS?
Javadoc forSystem.getenv(String name)
似乎暗示该方法依赖于平台,但我不清楚。我可以完全跳过将变量传递给 JVM,并getenv()
在使用操作系统设置环境变量的值后使用吗?
采纳答案by ChssPly76
System.getenv()is platform-independent by itself. Using your above example, you can most certainly write
System.getenv()本身与平台无关。使用上面的例子,你肯定可以写
String value = System.getenv("MY_ENV_VAR")
and it will work on both Linux and Windows. No reason to wrap this into java system property. That said, the "platform-dependent" part of getenv()
lies in the fact that different operating systems use different environment variables, like PATH on windows vs path on Linux. But as long as you're using your own variables and name them consistently (always uppercase, for example), you'll be fine.
它适用于 Linux 和 Windows。没有理由将其包装到 java 系统属性中。也就是说,“平台相关”的部分getenv()
在于不同的操作系统使用不同的环境变量,例如 Windows 上的 PATH 与 Linux 上的路径。但是只要您使用自己的变量并始终如一地命名它们(例如,总是大写),您就可以了。
回答by Alexander Torstling
How I interpret the java tutorial on thisis that getenv works in a platform independent way, but that you have to keep in mind that variables are not consistently named across platforms. Since you seem to set the var yourself, this does not apply to you.
我如何解释Java 教程是 getenv 以独立于平台的方式工作,但您必须记住,变量在不同平台上的命名不一致。由于您似乎自己设置了 var,因此这不适用于您。
回答by Tim Gilbert
Yes - getEnv() will just return the name of the environment variable, and you can set it in whatever way is appropriate to the platform you're running on (typically via a launch batch file on Win32). It's good practice to fall back on sensible defaults based on the platform (by inspecting System.getProperty("os.name")
), if it's possible you want to avoid having your users bother with needing to mess with environmental variables to run your software.
是的 - getEnv() 只会返回环境变量的名称,您可以以适合您正在运行的平台的任何方式设置它(通常通过 Win32 上的启动批处理文件)。System.getProperty("os.name")
如果可能,您希望避免让您的用户因为需要弄乱环境变量来运行您的软件而烦恼,最好使用基于平台的合理默认值(通过检查)。