1

I run Java on MacOS Monterey(12.6) and have two versions of JDKs installed:

user@Users-iMac ~ % ls /Library/Java/JavaVirtualMachines/
jdk-17.0.2.jdk  openjdk-8.jdk

I want to work with Java 17. My concern is that the command java -version generates the info that I do NOT want:

user@Users-iMac ~ % java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-bre_2023_01_22_03_30-b00)

whereas the command echo $JAVA_HOME displays the correct version that I DO want

user@Users-iMac ~ % echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home

How can I make `java -version make point to Java 17 instead of Java 8?

Thanks

Singham

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Singam
  • 443
  • 3
  • 12
  • I'm not too familiar with macs, but didn't they have a tool named `sdkman` that solves this problem? – Jorn Jun 20 '23 at 08:26
  • What does your PATH look like. If you have JAVA_HOME pointing at a correct JDK it should work, but if you have added something to your path then it might find a different version of java first. What is the output of `which java` – matt Jun 20 '23 at 08:31
  • Does this answer your question? [How to set JAVA\_HOME environment variable on macOS?](https://stackoverflow.com/questions/32517507/how-to-set-java-home-environment-variable-on-macos) – matt Jun 20 '23 at 08:32
  • 1
    The `JAVA_HOME` is something used by tools using Java to locate the JDK; it is not something that is actually used by Java itself, nor by your OS (unless you yourself added `$JAVAHOME$/bin` to the `PATH`). – Mark Rotteveel Jun 20 '23 at 08:34
  • @MarkRotteveel OSX will let you select java's using java home. It will always refer to a /system/path/java but different java's will run. – matt Jun 20 '23 at 08:35
  • @matt As far as I'm aware, that doesn't use `JAVA_HOME`, but some configuration tool (which IIRC changes a symlink), but I have been using sdkman on my work Mac for so long, I can't remember to alternative any more, and I'm using Windows at home. – Mark Rotteveel Jun 20 '23 at 08:39
  • 1
    Maybe this is a better answer, it will tell you what JDK's you have available ie correctly installed. https://stackoverflow.com/a/24657630/2067492 – matt Jun 20 '23 at 08:43
  • @MarkRotteveel When I had a mac, I didn't use JAVA_HOME for things beyond maven. Then I followed a question on here, and learned that JAVA_HOME can change the default jdk. – matt Jun 20 '23 at 08:49

4 Answers4

1

Don't play with PATH in this case, only with JAVA_HOME. The commands relative to java in /usr/bin/ are front-ends which use the JAVA_HOME variable to locate the correct binaries.

But if you mix JAVA_HOME=<java17> with a PATH=<java8>:... then obviously java command from java8 will be executed.

At least, put /usr/bin in front of <java8> in your PATH. Best is to never have a reference to a java installation in PATH.

As a macOS user, I never play with PATH when I use java, only JAVA_HOME.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 1
    This is the correct answer. Add `export JAVA_HOME=$(/usr/libexec/java_home -v 17)` to your `~/.bash_profile`. – Alex Jun 21 '23 at 15:50
0

My workaround is settting up my environment by editing .bash_profile file:

export JAVA_HOME=`/usr/libexec/java_home -v 17`
export PATH=$PATH:$JAVA_HOME/bin

It is better to automate using an alias or an executable which comes handy for example when you have to set-up a different Maven version also for the particular Java:

cp ~/.bash-profile-java-11 ~/.bash-profile
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
-1

I had the same usecase where I have to switch to different java version for different projects

This is what I use to switch between different java versions

alias j8='export JAVA_HOME=$JAVA_8_HOME'
alias j19='export JAVA_HOME=$JAVA_19_HOME'
alias j11='export JAVA_HOME=$JAVA_11_HOME'

you can define JAVA_x_HOME with specific path

e.g. JAVA_8_HOME --> /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

-1

Just to get rid of some of the confusion about aliases and to show how JAVA_HOME and PATH interconnect, the following alias approach is what I use. This should be defined where it will be sourced automatically, which for me is ~/.bash_aliases, but might be something different for MacOS

alias j8='export JAVA_HOME=/usr/lib/jvm/bellsoft-java8-full-amd64;export PATH=$JAVA_HOME/bin:$PATH'
g00se
  • 3,207
  • 2
  • 5
  • 9
  • 1
    OSX uses (or at least used to) JAVA_HOME to delegate which java gets run. It has a stub in /usr/bin/java (or something similar) which you update JAVA_HOME correctly that stub will start the correct java. You don't need to change the PATH at all. Although if you have already changed the path, then JAVA_HOME won't change the version of java being run. – matt Jun 20 '23 at 09:17