Pages

Monday, July 6, 2020

How to install Java 8 on Mac? Installing Java 8 on Latest Mac OS X (HomeBrew Guide)

1. Introduction


In this tutorial, You'll be learning how to install Java 8 or the latest versions of java in Mac OS.
If you have got the new mac book then you are existed to install java.

This is a very easy process and will show you step by step in an easy manner.

First, let us start with the Manual Installation and next go with the HomeBrew approach which is simpler managed automatically with few commands.

Before that download the latest java from Oracle or OpenJDK official websites.

Read more on Why Java is preferred?

How to install Java 8 on Mac? Installing Java 8 on Latest Mac OS X (HomeBrew Guide)


2. Manual Installation of java 8 or Higher Versions


Let us go through the manual installation of Java 14 or 8. To run the following you would need the admin access.

2.1 Download the latest JDK or needed version for you.

2.2 Copy the tar file to the location "/Library/Java/JavaVirtualMachines"
$ cd /Library/Java/JavaVirtualMachines

$ pwd
/Library/Java/JavaVirtualMachines

$ sudo cp ~/Downloads/openjdk-14_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines

$ pwd
/Library/Java/JavaVirtualMachines

2.3 Extract the contents here.
$ sudo tar xzf openjdk-14_osx-x64_bin.tar.gz
2.4 Delete the tarfile if you no need to keep the backup.
$ sudo rm openjdk-14_osx-x64_bin.tar.gz
2.5 Find the java version installed
$ /usr/libexec/java_home -v14

/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home
2.6 Add the java home path to the ".bash_profile" file.
open the file with the VI editor. Enter into edit mode by pressing 'i' and then add the below line. Press :wq to save and come out of VI editor.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home
2.7 Source bash_priofile file
$ source ~/.bash_profile
2.8 Test the version installed on mac properly or not using "java --version".
$ java -version
openjdk version "14" 2020-03-17
OpenJDK Runtime Environment (build 14+36-1461)
OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home

3. Installing java 8 using HomeBrew

HomeBrew provides simpler way to install and manage the software and tools required on mac without any manual intervention.
If you are new then please see the article on HomeBrew installation and  Commands shown in the previous article.

3.1 Install Homebrew or update it if you have already on mac.
The update will take some time if you have done it a long time ago.

$ brew update
==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
################################################################################################################################################################ 100.0%
3.2 Add open jdk to the brew

By default, all repositories are not added to the brew. So, to add the additional repos you need to use the brew tap command.

$ brew tap adoptopenjdk/openjdk
Updating Homebrew...
3.3 Find all JDK versions are on Brew
$ brew search jdk
==> Formulae
openjdk                                                                           openjdk@11
==> Casks
adoptopenjdk                     adoptopenjdk12                   adoptopenjdk13-openj9            adoptopenjdk14-openj9-jre-large  adoptopenjdk9
adoptopenjdk10                   adoptopenjdk12-jre               adoptopenjdk13-openj9-jre        adoptopenjdk14-openj9-large      jdk-mission-control
adoptopenjdk11                   adoptopenjdk12-openj9            adoptopenjdk13-openj9-jre-large  adoptopenjdk8                    oracle-jdk
adoptopenjdk11-jre               adoptopenjdk12-openj9-jre        adoptopenjdk13-openj9-large      adoptopenjdk8-jre                oracle-jdk-javadoc
adoptopenjdk11-openj9            adoptopenjdk12-openj9-jre-large  adoptopenjdk14                   adoptopenjdk8-openj9             sapmachine-jdk
adoptopenjdk11-openj9-jre        adoptopenjdk12-openj9-large      adoptopenjdk14-jre               adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-jre-large  adoptopenjdk13                   adoptopenjdk14-openj9            adoptopenjdk8-openj9-jre-large
adoptopenjdk11-openj9-large      adoptopenjdk13-jre               adoptopenjdk14-openj9-jre        adoptopenjdk8-openj9-large
Venkateshs-MacBook-Pro-2:JavaVirtualMachines venkateshn$ 
It is showing with a blue tick for version 11. That means, My machine is already having the java 11 version.

3.4 Choose the right version and run one of the following commands.

brew cask install adoptopenjdk8
brew cask install adoptopenjdk9
brew cask install adoptopenjdk10
brew cask install adoptopenjdk11
brew cask install adoptopenjdk12
brew cask install adoptopenjdk13
brew cask install adoptopenjdk14

3.5 install JDK 14 version

This will take quite some time based on your internet speed.
$ brew cask install adoptopenjdk14
Updating Homebrew...
==> Downloading https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.1%2B7/OpenJDK14U-jdk_x64_mac_hotspot_14.0.1_7.pkg
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/233878254/64ac0f80-830e-11ea-8bcd-a9f8ee45c527?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'adoptopenjdk14'.
==> Installing Cask adoptopenjdk14
==> Running installer for adoptopenjdk14; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
installer: Package name is AdoptOpenJDK
installer: Upgrading at base path /
installer: The upgrade was successful.
package-id: net.adoptopenjdk.14.jdk
version: 14.0.1+7
volume: /
location: Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk
install-time: 1593938817
🍺  adoptopenjdk14 was successfully installed!
3.6 How to see the installed Java location?
Run from terminal "/usr/libexec/java_home -V" command which shows all java versions installed on this machine already.
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (6):
    14.0.1, x86_64: "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    12.0.1, x86_64: "Java SE 12.0.1" /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
    1.8.0_252, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
    1.8.0_161, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
3.7 Verify the java version
$ java --version
openjdk 11.0.7 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10)
OpenJDK 64-Bit Server VM (build 11.0.7+10, mixed mode)
Currently, I have set JAVA_HOME to java 11 version. Because of this, we are seeing the java 11 version even though installed the latest java 14.
That's all. The latest version is installed.

4. How to change or Switching between Java versions

In the above sections, We've already shown you how to install the different versions of java using HomeBrew and Manual installation steps.

If you have installed any version of java and there are some cases you need to change the java version based on your project needs.

Note: In the market, there is a tool jenv.be to switch between the java versions but I prefer to use Export JAVA_HOME in .bash_profile file.

4.1 First, let us see how to see the installed versions of java.

Open the terminal and hit the "/usr/libexec/java_home -V" command and this shows all versions installed with locations.
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (6):
    14.0.1, x86_64: "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    12.0.1, x86_64: "Java SE 12.0.1" /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
    1.8.0_252, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
    1.8.0_161, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
4.2 Open the .bash_profile file

$ vim ~/.bash_profile

Add the following to the .bash_profile file
export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8)
export JAVA_HOME_11=$(/usr/libexec/java_home -v11)
export JAVA_HOME_14=$(/usr/libexec/java_home -v14)

# Java 8
export JAVA_HOME=$JAVA_HOME_8

# Java 11
# export JAVA_HOME=$JAVA_HOME_11
Save the file and come out of VI editor.

4.3 Based on the above change, we are setting JAVA_HOME to java 1.8 version.

4.4 Reload the .bash_profile
$ source ~/.bash_profile
4.5 Verify the java version
$ java -version

openjdk version "1.8.0_251"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_251-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.251-b08, mixed mode)

5. Conclusion

In this article, You have seen how to install Java on Mac OS with manual and HomeBrew Installation with step by step.

As well as seen how to change the java version as you needed from the terminal with few commands.



Ref

Oracle java 8 steps

Open JDK

HomeBrew Commands

jenv.be

How to change the Java version on Mac OS

No comments:

Post a Comment

Please do not add any spam links in the comments section.