Search code examples
javajava-home

Why JVM work when there is not JAVA_HOME?


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


Solution

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