Java 系统环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9677346/
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
Java System Environment Variable
提问by Lee Chee Kiam
Does Java have a default System Environment Variable that will always be read/appended when we set it? CATALINA_OTPS
/JAVA_OPTS
etc seems only for TOMCAT/JBOSS etc.
Java 是否有一个默认的系统环境变量,当我们设置它时,它总是会被读取/附加?CATALINA_OTPS
/ JAVA_OPTS
etc 似乎只适用于 TOMCAT/JBOSS 等。
- I do not want to set it through Java system properties (which is passed in via -Dprop1=value1 -Dprop2=value2) as it involves shell/batch script.
- It should work across OS, like double click jar file in Windows.
- It should work across different JREs (Sun, IBM, OpenJDK etc).
- It should not involve extra coding.
- It should work in most libraries configuration file, like setting log4j level ${LOG_LEVEL}.
- 我不想通过 Java 系统属性(通过 -Dprop1=value1 -Dprop2=value2 传入)设置它,因为它涉及 shell/批处理脚本。
- 它应该跨操作系统工作,就像在 Windows 中双击 jar 文件一样。
- 它应该适用于不同的 JRE(Sun、IBM、OpenJDK 等)。
- 它不应该涉及额外的编码。
- 它应该适用于大多数库配置文件,例如设置 log4j 级别 ${LOG_LEVEL}。
Update: Added item # 4 and 5. Remove OS from title to make my question clearer.
更新:添加了第 4 项和第 5 项。从标题中删除操作系统以使我的问题更清晰。
Update 2: After looking at Perception's answer, it seems like my item 2 and 3 can be achieved via System.getenv
. How to achieve item 4 and 5?
更新 2:查看 Perception 的答案后,似乎我的第 2 项和第 3 项可以通过System.getenv
. 如何实现第 4 项和第 5 项?
Here is example of scenario:
Imagine now JAVA_DEFAULT_OPTS
is an environment variable that will be read by Java as it has now become the standard. On development desktop machine, I set JAVA_DEFAULT_OPTS=-DLOG_LEVEL=DEBUG -Xmx384m
; On production server machine, customers set JAVA_DEFAULT_OPTS=-DLOG_LEVEL=INFO -Xmx1024m
. When I/users double click the jar file on Windows, the application will run will different log4j level and max memory heap size.
下面是一个场景示例:想象一下,现在JAVA_DEFAULT_OPTS
是一个环境变量,它将被 Java 读取,因为它现在已成为标准。在开发台式机上,我设置了JAVA_DEFAULT_OPTS=-DLOG_LEVEL=DEBUG -Xmx384m
; 在生产服务器机器上,客户设置JAVA_DEFAULT_OPTS=-DLOG_LEVEL=INFO -Xmx1024m
. 当我/用户在 Windows 上双击 jar 文件时,应用程序将运行不同的 log4j 级别和最大内存堆大小。
采纳答案by RealHowTo
There is a special environment variable called _JAVA_OPTIONS, its value will be picked up by the JVM (java.exe).
有一个特殊的环境变量叫做_JAVA_OPTIONS,它的值会被 JVM (java.exe) 获取。
In Windows:
在 Windows 中:
set _JAVA_OPTIONS=-Xms64m -Xmx128m -Dawt.useSystemAAFontSettings=lcd
In Linux:
在 Linux 中:
export _JAVA_OPTIONS='-Xms64m -Xmx128m -Dawt.useSystemAAFontSettings=lcd'
For Java Web Start it's JAVAWS_VM_ARGS. For javaw.exe (Applet), it's _JPI_VM_OPTIONS.
对于 Java Web Start,它是JAVAWS_VM_ARGS。对于 javaw.exe (Applet),它是_JPI_VM_OPTIONS。
回答by Perception
Java has a standard OS system environment variable that is always set when the JVM is launched:
Java 有一个标准的 OS 系统环境变量,它总是在 JVM 启动时设置:
- os.name
- os.arch
- os.version
- 操作系统名称
- os.arch
- 操作系统版本
All accessible via 'System.getProperty(propertyName)`
所有可通过“System.getProperty(propertyName)”访问
If you need anything more than this you could always use the Management API.
如果您需要更多的东西,您可以随时使用Management API。