JAVA_OPTS Environment Variable

Sheeraz Gul Oct 12, 2023
JAVA_OPTS Environment Variable

JAVA_OPTS is an environment variable used to pass custom settings to Java Virtual Machine. This tutorial demonstrates the use of JAVA_OPTS.

JAVA_OPTS Environment Variable

JAVA_OPTS is a standard environment variable used to set the custom setting for Java Virtual Machine. For example, if we set JAVA_OPTS=-Xmx512m in the tomcat, the startup script will execute org.apache.tomcat.Servert –Xmx512m, the –Xmx is the maximum heap size.

Set JAVA_OPTS Environment Variable

We can set the heap size with JAVA_OPTS using the Set command. Run the following command to set the heap size with JAVA_OPTS.

set JAVA_OPTS="-Xms128m -Xmx1024m"

Where -Xms is the minimum heap size and -Xmx is the maximum heap size.

Run the command prompt as administrator and run the command above. You can also set the heap size according to your requirement.

Get JAVA_OPTS Environment Variable in Java

Once the JAVA_OPTS is set, we can get the settings in Java by using the System.getenv() method. If the JAVA_OPTS is not set, the method will return null.

See example:

package delftstack;

public class Delftstack {
  public static void main(String[] args) {
    String Print = System.getenv("JAVA_OPTS");
    System.out.println(Print);
  }
}

The code above will get the JAVA_OPTS settings. See output:

-Xms128m -Xmx1024m
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Environment