21 Haziran 2017 Çarşamba

How to install an additional java on MacOS?

Java 8 is already built on my Mac, but I also need Java 7 to run some projects properly. Addition to this, switching between these versions should be easy, because I may need to switch it based on a project I work on.

Actually, the fastest and right way is to install Java 7 and manage versions of Java via "homebrew". However, I don't know why, Oracle put Java 7 installer under the authentication. It needs username and password. Because of this, related installer cannot be found on homebrew repos.

Not the fastest way, but there is only one way left, MANUEL SETUP.
1) Uninstall jdk8:
sudo rm -rf /Library/Java/*
sudo rm -rf /Library/PreferencePanes/Java*
sudo rm -rf /Library/Internet\ Plug-Ins/Java*
2) Install jdk7:
You must have oracle account. Enter your username & password on Oracle Java 7 download page and download/install it.

3) Reinstall jdk8:
You don't need oracle account for this.

4) Prepare environment variables:
Add something below into .profile(.bash_profile) file on your home directory.
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'

#default java8
export JAVA_HOME=$JAVA_8_HOME
5) Check the version of your java installed on your Mac.

References:
  1. https://stackoverflow.com/a/26252993
  2. https://github.com/caskroom/homebrew-cask/issues/9447
  3. https://stackoverflow.com/a/30412014