Mac / Linux Terminal Switch between or change your Java Version for a session.

Walter-Tscharf-Development
2 min readJun 23, 2022

This article is for people, who want to change their java version of a temporary terminal session. That means if you close the terminal window or disconnect from your ssh you will lose the version and the next time the default one will be loaded again.

Steps:

  1. We need to install Java 8 via the Terminal. Therefore open the terminal. Execute the following command:
brew install cask adoptopenjdk8

I already have it this is why it is saying re-install.

2. Afterwards we need to switch our current terminal session to this Java version therefore follow the following steps. Execute:

/usr/libexec/java_home -V

Make a note of the new installed java version in this case -> 1.8.0_292

3. Switch the Java version of the current Terminal window with:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_292`

4. Check if you are really on the Java 1.8 with:

java -version

Important now is that you don’t close this window, where you set your Java 1.8 version. Because the java version is only set for this session.

Conclusion

You only need a few commands to set your java version.

I hope the article helped. Happy coding.

--

--