Vladimir Fokow
Vladimir Fokow

Reputation: 3883

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:


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

Upvotes: 1

Views: 857

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191743

It depends on the program.

Most programs that depend on Java follow logic like this

if $JAVA_HOME ; then
  use JAVA_HOME/bin/java
elif $(which java); then
  use java command on PATH
else
  error
fi

That being said, to be consistent, you should set it , and using SDKman or Homebrew does that for you

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

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.

Upvotes: 5

Related Questions