-2
$ env
...

When I try to print my shell environment on macOS, I find there is not a JAVA_HOME, but my JVM still works.

So I am very confused, I just want to understand how JVM finds the location of JDK when there is not indicated from shell env.

Then my JDK location is /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk, if I move out this folder, JVM will not work.

I know this is useless and very stupid. But I just want to fully understand how does this work, please help.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • 1
    When you start a program, the operating system allows the program to find out where its executable was loaded from, this is just a basic function of the OS. The JVM does not use the JAVA_HOME environment variable, it's not necessary to set it for the JVM's sake. – Jesper Feb 21 '19 at 10:35

1 Answers1

2

The JVM does not require JAVA_HOME environment variable. Once you install it in the /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk the internal bin directory is added to the PATH environment variable (or some other mechanism like update-alternatives is used to put java executable in the system PATH).

It's usually 3rd party tools that require JVM (e.g. Apache Maven or Tomcat) that use JAVA_HOME environment variable to find the installed JVM.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111