12

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.

  1. 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.
  2. It should work across OS, like double click jar file in Windows.
  3. It should work across different JREs (Sun, IBM, OpenJDK etc).
  4. It should not involve extra coding.
  5. It should work in most libraries configuration file, like setting log4j level ${LOG_LEVEL}.

Update: Added item # 4 and 5. Remove OS from title to make my question clearer.

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?

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.

Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87

2 Answers2

18

There is a special environment variable called _JAVA_OPTIONS, its value will be picked up by the JVM (java.exe).

In Windows:

set _JAVA_OPTIONS=-Xms64m -Xmx128m -Dawt.useSystemAAFontSettings=lcd

In 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.


edit 20201213

_JAVA_OPTIONS environment variable was ok but was not documented or supported.

Since it is not standardized, other vendors have their own names e.g. IBM_JAVA_OPTIONS. Leading underscore names are private by convention so it's not a good idea to standardize the usage of _JAVA_OPTIONS.

That's why JAVA_TOOL_OPTIONS should be the preferred choice.

ref : https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#tooloptions

RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • It works. At least in Windows with Sun JRE. Linux should work as well (http://stackoverflow.com/a/1508684/418439). Anyone let me know if Mac also work. Is this an easter egg? It doesn't even officially documented at http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html. I should ask this few years back... :( – Lee Chee Kiam Mar 13 '12 at 03:27
  • @CKLee , you can find a mention of it in the Java 2D documentation, https://docs.oracle.com/javase/8/docs/technotes/guides/2d/flags.html and http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/plugin.html – RealHowTo Mar 26 '15 at 21:50
1

Java has a standard OS system environment variable that is always set when the JVM is launched:

  • os.name
  • os.arch
  • os.version

All accessible via 'System.getProperty(propertyName)`

If you need anything more than this you could always use the Management API.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Do you mean Java Management Extensions (JMX) API? How to do it without extra coding? – Lee Chee Kiam Mar 13 '12 at 02:00
  • Included a link to Management API. Not sure what you mean by 'extra coding', you have to write at least the one or two lines of code required to access the information you need. – Perception Mar 13 '12 at 02:04