_JAVA_OPTIONS will not always work for your Java code

It’s good to know, that _JAVA_OPTIONS will not always work in your Java code. Especially, when you elevate privileges using sudo.

Let’s say we have this simple code

public class Simple {
  public static void main(String [] arg) {
    System.out.println("Hello from Simple!");
  }
}

and we run it following way

> export _JAVA_OPTIONS="-Xms1G"
> java Simple
Picked up _JAVA_OPTIONS: -Xms1G
Hello from Simple!
> sudo java Simple
Password: πŸ”‘
Hello from Simple!

As you can see, in case of second execution _JAVA_OPTIONS were not picked up. The reason for not picking it up follows

/* ./hotspot/share/runtime/arguments.cpp */ 

// Don't check this environment variable if user has special privileges
// (e.g. unix su command).
if (buffer == NULL || os::have_special_privileges()) {
  return JNI_OK;
}