Jdk 7 On Homebrew

05 Feb 2019

Homebrew is a package management software to install various software on macOS, however, we can also use this open-source Brew on Windows WSL and Linux operating systems such as Debian, Ubuntu, Linux Mint, Fedora, CentOS, RedHat and more Homebrew is the best package management tool on macOS. When used on Linux, Homebrew is known as Linuxbrew. 3 How to check which JDK version is currently used by Mac OS Using java -version or javac -version to show the JDK version used by system. $ java -version java version '1.8.0121' Java(TM) SE Runtime Environment (build 1.8.0121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) $ javac -version javac 1.8.0121.

MacOS has OpenJDK installed by default however I prefer to use Oracle’s versionof JDK because its the official version. I don’t want to install it the same wayOracle instructs it on their docs as I find it very tedious. I’m a guy who lovesautomating stuff so I prefer to install it via Homebrew. I frequently do a cleaninstall on my Mac every time there is a new version of OSX so I have to installJDK again and again. I’d rather just run a single installation script instead ofheading over to Oracle’s website and following their instructions.

Steps to install and configure the Oracle JDK:

Homebrew and Cask

Homebrew is a package manager for Mac and has always been my preferred way toinstall my command line tools because I can integrate it with my setup scripts.To install it I’ll run

Homebrew

then I’ll install Homebrew Cask which is an extension of Homebrew. It makesthe installation of large binaries and graphical applications simpler.

JDK Installation

Before I install the JDK, I’ll check first which version it will install bydefault. I’m very picky about the version because most of the time I just useJava for Android development. I also prefer the older and more stable version ofJDK so I run

which will output

This means that the latest version is JDK 11. I can install it now by running

but I prefer to install JDK 8 over 11 so instead I’ll run

Setup Java_HOME environment variable

Once installed, I will set the JAVA_HOME environment variable by editing my.bash_profile

and inserting this line

and applying these changes by running

Verifying Installation

Homebrew

Now to confirm if the installation was sucessful I’ll run this command.

If the installation is successful, its output would be similiar to this

This tells me that I have installed the Oracle version of the JDK. However ifthe output is like this

then I may have failed to install the JDK properly or the changes may not havebeen applied yet because I can see that OpenJDK is still being used. I’ll try tofix this by restarting my Mac then running “java -version” again.

Automating installation with a script

Below is a simple script to automate the installation of the latest Oracle JDK.

That’s it! Now you can automate your JDK installation on you Mac by running thescript.

Related Posts

Please enable JavaScript to view the comments powered by Disqus.

During one of AWS Lambda exploration projects, I need to use Amazon’s swagger-import-tool for uploading swagger documents to AWS. The problem is that this tool has a dependency on Java 8, however, my installed JDK is 7, and I have to keep it for all existing Java projects. It seems that I need to have something like NVM (Node Version Manager) … this time for JDKs.

After some Google searches, a promising solution is to use jEnv – a command line tool to set JAVA_HOME on the fly.

Homebrew Cask

On Mac, Homebrew is the de-facto package manager, and Homebrew Cask is the app manager. I’m going to use Cask to install Java 7 and 8.

Installing A Java 8 JDK On OS X Using Homebrew - The Lone C++ ...

Install Homebrew Cask first if you haven’t:

If your brew or cask is outdated, update and upgrade:

Install Java 7 and 8

Brew Install Java: Installing Java On MacOS Using Homebrew ...

People on the Stackoverflow cautioned not to install 8 until 7 is installed. So we are going to install JDK 7 first.

Unlike other version managers such as NVM, jEnv itself doesn’t install JDKs. You have to do it yourself. Luckily, Homebrew Cask made this task really easy. But before doing that, let’s check if we already have JDK 7 installed by Homebrew Cask:

If Java 7 is installed, you should see something like this:

Otherwise, install Java 7:

If you run into permission issue, add sudo at the beginning of the above command.

As of today, Java 8 is the latest stable. Run the following command to install Java 8:

These two JDKs will be installed at the following directories. Your JDKs’ minor and patch versions might be different.

Enter jEnv

Now it is time to install jEnv:

Add the following lines to ~/.bash_profile. This will initialize jEnv.

See Full List On Github.com

jEnv doesn’t install JDKs, so we have to tell jEnv where to look for them. Type these commands to register JDKs in jEnv (replace the minor and patch versions with yours):

After that, run this command to list all registered JDKs:

The output will be something like this:

Jdk 7 On Homebrew Software

The version with an asterisk is the active version.

In my case, I need to keep JDK 7 as my default version, so I set the global version to 1.7:

GitHub - AdoptOpenJDK/homebrew-openjdk: AdoptOpenJDK HomeBrew Tap

And in my project, I set the local JDK version to 1.8:

The above command will create a .java-version file at project root. Its content is the version I just picked for this project:

References

Notes

To learn more about jEnv, here I list some references:

  • jEnv official site
  • The Stackoverflow thread
  • Andrew John’s blog post

Jdk 7 Homebrew


Comments are closed.