Home > OS >  Mac - how to install java 17
Mac - how to install java 17

Time:11-08

Could someone please let me know the steps to install Java on a Mac.

I did brew install java

I get this

Warning: openjdk 17.0.1 is already installed and up-to-date.
To reinstall 17.0.1, run:
  brew reinstall openjdk

If I do java -version, I get this.

openjdk version "13.0.8" 2021-07-20

If I have navigate to /Library/Java, I have 2 empty directories.

Where is java 17 installed??

CodePudding user response:

Java doesn't mind if you install multiple versions. This is often required; java is not backwards compatible (it tries to change little, but e.g. the java8 to java9 transition broke a ton of stuff, much of it needless and much of it not reasonably expectable or fixable by libraries and apps, so a bunch of java apps and libraries only run on java8 - just an example).

So, yes, you have installed JDK17. Also, yes, if you just run java without specifying which one you want, you so happen to get java13 here.

To see all installed javas, you can run:

/usr/libexec/java_home -V

to 'override', you can use something like (depends on which shell you're using on your mac):

export JAVA_HOME=`/usr/libexec/java_home -v 17`

(the backticks mean: Run this then take the output of it and treat that as the 'value' of the expression. here, assign it to the JAVA_HOME env var. -v 17 requests a path to java 17. The -V option lists all and is meant for your eyeballs, not for scripts. The -v option is mostly for scripting, and that's how we're using it here).

JAVA_HOME decides which java is used by some things, but the java you get when you just type java is /usr/bin/java, and that executable is actually just a wrapper that picks a java to run from amongst all installed versions. It uses JAVA_HOME to decide which java to actually run. There are wrappers for all the common commands (javac, too). You can always run e.g. which javac to see what that actually runs; you probably see /usr/bin/javac. Everything in /usr/bin is one of these wrapper thingies that looks at JAVA_HOME and then runs the binary it finds there.

  • Related