Skip to content

Instantly share code, notes, and snippets.

@jef

jef/jenv.md

Last active May 19, 2021
Embed
What would you like to do?
Select and download Java 8 and Java 11 for macOS

jenv

No longer maintaining: instead of using the below, try out https://asdf-vm.com. Particularly, https://github.com/halcyon/asdf-java.

Using Java in the terminal can be tricky when you're dealing with multiple projects with different Java versions. Projects like fnm make this easy. This is dumbed-down version of these type of software.

👉 Checkout the dev.to article here

asciicast

Installation

Terminal

Save the following script inside your $HOME directory as .jenv

#!/bin/bash

java_dir="/Library/Java/JavaVirtualMachines"
java8_dir="$java_dir/adoptopenjdk8"
java11_dir="$java_dir/adoptopenjdk11"
java8_hotspot_tar="https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u275-b01/OpenJDK8U-jdk_x64_mac_hotspot_8u275b01.tar.gz"
java11_hotspot_tar="https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.9.1%2B1/OpenJDK11U-jdk_x64_mac_hotspot_11.0.9.1_1.tar.gz"

export JAVA_HOME="$java11_dir/Contents/Home"

_download_java8() {
    local _force="$1"

    if [ ! -d "$java8_dir" ] || [ "$_force" = "force" ]; then
        _temp_dir=$(mktemp -d)
        sudo rm -rf "$java8_dir"
        echo "⏲  installing java 8"
        curl -Ls "$java8_hotspot_tar" | tar xz -C "$_temp_dir"
        sudo mv "$_temp_dir/jdk8u275-b01" "$java8_dir"
        echo "✅ completed"
    fi

    export JAVA_HOME="$java8_dir/Contents/Home"
}

_download_java11() {
    local _force="$1"

    if [ ! -d "$java11_dir" ] || [ "$_force" = "force" ]; then
        _temp_dir=$(mktemp -d)
        sudo rm -rf "$java11_dir"
        echo "⏲  installing java 11"
        curl -Ls "$java11_hotspot_tar" | tar xz -C "$_temp_dir"
        sudo mv "$_temp_dir/jdk-11.0.9.1+1" "$java11_dir"
        echo "✅ completed"
    fi

    export JAVA_HOME="$java11_dir/Contents/Home"
}

jenv() {
    echo "👀 password for moving to safe directory"
    sudo echo

    local _force="$1"
    select version in java8 java11 exit;
    do
        case $version in
            java8)
                _download_java8 "$_force"
                echo "🚀 java8 set!"
                java -version
                break
                ;;
            java11)
                _download_java11 "$_force"
                echo "🚀 java11 set!"
                java --version
                break
                ;;
            exit)
                echo "👋 exiting"
                break
                ;;
            *)
                echo "🚫 unknown selection"
                ;;
        esac
    done
}

Then add source "$HOME/.jenv to your .bashrc or .zshrc.

Reload your terminal or run source .bashrc or source .zshrc to ensure the script loads.

At this point, you should be able to run jenv in terminal to select Java 8 or Java 11.

IntelliJ

If you use IntelliJ, then these instructions will help guide you to an easier approach in switching JDKs.

Navigate to Project Structure using either Cmd + ; or going to File -> Project Structure...

Navigate to SDKs under Platform Settings and click the + button to add the newly created JDKs.

image

Either you'll find auto populated JDKs or you will need to add them manually by clicking Add JDK... and locating them in /Library/Java/JavaVirtualMachines/adoptopenjdk{8,11}/.

image

Giving them nice names help.

image

Navigate to Project under Project Settings and choose the recently added SDK that's relevant to your projects target JDK.

image

Make sure your Project language level is set to SDK default.

image

Now you're all set within IntelliJ! 🚀

Caveats

Sometimes the Gradle wrapper can have some difficulties picking up the new SDK. You can alleviate this pain point by going to Preferences via Cmd + , or IntelliJ -> Preferences and navigating to Gradle under Build, Execution, Development -> Build Tools.

Set the Gradle JVM under Gradle to Project SDK

image

Voila! 🔮 You're definitely ready to go now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment