DEV Community

Cover image for Installing Java on MacOS using Homebrew and JEnv
Gabriel
Gabriel

Posted on

Brew Install Java Installing Java on MacOS using Homebrew and JEnv

I think its fair to say that going through the process of installing Java on any machine is in of itself a bit confusing. For those that come from NodeJS, installing a new version of Node is as easy as installing nvm and running nvm install [some nodejs version] and calling it a day.

The goal of this post is to show that we could do just that with Java, install a package to allow us to toggle between Java versions and set our JAVA_HOME path automatically for us just by running a few simple commands.

Before we start, ensure that you have homebrew installed and that you have updated all dependencies (ie. run brew upgrade)

  1. First, we need to install JEnv using homebrew, run the following command:

    $ brew install jenv 
    
  2. Next, ensure that jenv is listed as a command on your shell, if you are using bash, then run

    $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
    $ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
    

    If using .zshrc, then run the following

    $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
    $ echo 'eval "$(jenv init -)"' >> ~/.zshrc
    

    Note: It's important that now you restart your bash terminal or run . ~/.zshrc to reload your .zshrc now that we have added a new command shortcut.

  3. Now let's go ahead and download a brew cask for the jdk version that we are after. Before we go and download the version that we need, ensure that you run the following command to add the brew repo to your machine (https://github.com/AdoptOpenJDK/homebrew-openjdk). Note that to install oracle-jdk , check out the instructions listed here https://formulae.brew.sh/cask/oracle-jdk. Note that the following steps will be only to install the AdoptOpenJDK and not the Oracle JDK. If you need some info on the differences between each type, check out https://www.openlogic.com/blog/java-experts-openjdk-vs-oracle-jdk#:~:text=The biggest difference in OpenJDK,JDK requires a commercial license.&text=Since January 2019%2C businesses now,order to receive software updates.

    $ brew tap AdoptOpenJDK/openjdk
    
  4. Now that we have the AdoptOpenJDK, let's run the following command to install Java 8 (jdk 1.8.x )

     $ brew cask install adoptopenjdk8
    
  5. Once the installation completes, we'll then follow up with jenv to add this version into our list of available version to be able to toggle between versions. Let's ensure that we know the location where the jdk package was installed, for MacOS Catalina, that should be under the following directory /Library/Java/JavaVirtualMachines

  6. If we cd into /Library/Java/JavaVirtualMachines we should see the following:

    adoptopenjdk-8.jdk
    
  7. If the above is correct, we'll go ahead and run the following command to add the location of this version to jenv

    $ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    
  8. To confirm that the version is now available on jenv, run the following command jenv versions, you should see the following output:

    openjdk64-1.8.0.252
    
  9. Now to add Java 8 as your overall global java version, run the following command:

    jenv global openjdk64-1.8.0.252
    
  10. Now, run java -version and you should see the following output:

    openjdk version "1.8.0_252"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)
    

    Note: If you still see your old version or nothing at all, ensure that you restart your bash terminal (or .zsrh) to pull in the new path to your Java home (this is set by jenv itself, you don't need to do anything).

That's it! Hopefully it was painless and straightforward, next time you need to toggle between versions, just run jenv {version} and you're all set.

You could also set versions of Java specific to your shell terminal, or directory, simply replace global with local (if you are within a specific directory) or shell for adding a version to specific to the shell you're running.

Resources:

Top comments (6)

Collapse
 
ramuta profile image
Matej Ramuta • Edited

Hey, I got this error at step 7:

ln: /Users/username/.jenv/versions/openjdk64-1.8.0.275: Not a directory
Enter fullscreen mode Exit fullscreen mode

For some reason, the .jenv folder has not been created.

The workaround is to create both the .jenv and the .jenv/versions folders:

mkdir ~/.jenv
mkdir ~/.jenv/versions
Enter fullscreen mode Exit fullscreen mode

Everything else then worked like a charm. Thanks for the tutorial! ;)

Collapse
 
amatern profile image
amatern

This was very helpful! I only had one minor difference:

Error: Calling brew cask install is disabled! Use brew install [--cask] instead.

me@mac ~ % brew install --cask adoptopenjdk8

Which worked great, and obviously the openjdk version has changed... but thanks again for a great guide!

Collapse
 
zejnilovic profile image
Saša Zejnilović

I don't want to be that guy, but I think there is a typo in the Title. jenv vs jevn

Collapse
 
gabethere profile image
Gabriel

Thank you for being that guy! :) Appreciate the feedback, I just corrected it.

Collapse
 
h2oearth profile image
Daniel Cabrera

Thx for Sharing Gabriel! I was not aware of jenv! It really save my day :-)

Collapse
 
devopskang profile image
Joe Kang

Thank you for the write-up. This was helpful when configuring Jenkins to use the correct Java version for the build.