Search code examples
javamacosjava-home

Do I need to set JAVA_HOME on MacOS?


I'm fairly new to Java. Many of the tutorials on installing Java on a Mac recommend setting up the JAVA_HOME environment variable. However, Java works fine on my computer with the JAVA_HOME variable being empty!

I have successfully used Java from the command line (using javac and java), in IntelliJ IDEA (there in the "Project Structure Settings" I've set up the "JDK home path" to /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home) and in Sublime Text to compile and run Java files.

So, my questions are:

  • Why does Java work without the JAVA_HOME on my Mac?
  • Do I need to set up JAVA_HOME at all? What would be the consequences of not having it? Maybe it is not essential for some things, but is required for other things to work? Then what are these things?
  • Is this behavior different on other operating systems?

Outputs of some commands are:

echo $JAVA_HOME outputs an empty line

echo $CPPFLAGS outputs an empty line

which java

/usr/bin/java

echo $PATH contains /usr/bin


Solution

  • Why does Java work without the JAVA_HOME on my Mac?

    Setting JAVA_HOME is not required for Java to work in your system. Some applications (e.g. Tomcat, Maven etc.) however look for JAVA_HOME system variable and if its value is not set, they may prompt you to do so.

    Do I need to set up JAVA_HOME at all? What would be the consequences of not having it? Maybe it is not essential for some things, but is required for other things to work? Then what are these things?

    Already answered above.

    Is this behavior different on other operating systems?

    It's same across all operating systems.