_JAVA_OPTIONS Environment Variable in Java

Mohd Mohtashim Nawaz Mar 07, 2022
  1. Types of Java Options
  2. _JAVA_OPTIONS Environment Variable in Java
  3. JAVA_TOOL_OPTIONS Environment Variable in Java
  4. Difference Between JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS in Java
  5. Conclusion
_JAVA_OPTIONS Environment Variable in Java

The _JAVA_OPTIONS is an environment variable that you can use to pass the Java options to a JVM process. The passed Java options are treated similarly to the command line arguments by the Java Virtual Machine.

In this article, we will understand different types of Java options. We will also see the _JAVA_OPTIONS and the JAVA_TOOL_OPTIONS, the difference between the JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS environment variables.

Types of Java Options

Java defines three different types of options. Java differentiates between options based on their support in different Java versions and their usage.

Let us understand each of these options’ categories.

  1. Standard Options - Java guarantees that all Java versions support standard options. Java enables the use of these options for common actions such as:
    1. Setting the classpath variable.
    2. Checking the version of Java Runtime Environment (JRE).
    3. Toggling the verbose output, etc.
  2. Extra Options - Java does not guarantee the support of these types of options in all versions. These options are specifically related to JavaHotSpot Virtual Machine and can change.
    Java uses these options to set specific variables.
  3. Advanced Options - Java provides advanced options for developers. Developers can use these options to tune JVM, JRE, JIT, and Garbage Collector.

There are four different types of advanced options. These are given as follows.

  1. Runtime Options - Developers can use these options to tune the run time of JVM.
  2. JIT Compiler Options - Developers can use these options to control the Just In Time (JIT) Java compiler.
  3. Serviceability Options - Java defines these options for debugging and gathering system information.
  4. Garbage Collection Options - Developers can control the garbage collection mechanism of Java using these options.

_JAVA_OPTIONS Environment Variable in Java

Java defines several environment variables to pass the Java options to the JVM during the execution of a process. The _JAVA_OPTIONS is one such environment variable.

Java treats the arguments you pass using this environment variable similar to the command line arguments.

In fact, as per the documentation of Java by Oracle, Java provides these options so that command-line arguments can be passed to programs in those execution environments where a direct command line is not available.

You can pass multiple arguments where each argument is separated by white space. If your argument contains white space, you should enclose it within quotes.

You should be careful while using the _JAVA_OPTIONS because the program’s execution will fail if the arguments are invalid.

Let us see an example of passing the variable’s option to enable the log tracing.

export _JAVA_OPTIONS='-Dsun.java2d.trace=log'

JAVA_TOOL_OPTIONS Environment Variable in Java

The JAVA_TOOL_OPTIONS is the successor of the _JAVA_OPTIONS. The _JAVA_OPTIONS environment variable is not standardized in the JVM specification. Therefore, different vendors have their names for this variable.

However, the JAVA_TOOL_OPTIONS variable is standardized in the JVM specification. You should always prefer it over the _JAVA_OPTIONS.

This is because it handles the white space issues and quotations better.

Difference Between JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS in Java

The JAVA_TOOL_OPTIONS and the JDK_JAVA_OPTIONS are very similar as Java enables using both of the environment variables to pass the Java options to JVM.

As we have already seen, all the rules and methods of passing arguments using JAVA_TOOL_OPTIONS apply to JDK_JAVA_OPTIONS as well.

However, the main difference between these variables is as given below.

  1. Java launcher reads the arguments passed using JDK_JAVA_OPTIONS. Therefore, we can not pass options that cause the launcher to exit and specify JAR or the main class.

    Therefore, we can use this variable to pass options used during the run time at the launcher level.

  2. The Java compiler and JAR also read the JAVA_TOOL_OPTIONS. Therefore, we can use this variable to set options to tune the execution.

    For example, we can set flags using this variable.

Conclusion

This article has discussed Java’s JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS, and _JAVA_OPTIONS environment variables. We have also discussed the differences between the use of these environment variables.