Java installation (Mac, Homebrew, multiple version control)

Introduction

This article is for those who are worried about ** Java installation method ** and ** version to install ** on Mac.

There are three versions of Java, 8, 11 and 14.

"8 is fine now, but you may want to make it 14 in the future ~"

When you think about it, you're wondering which version to install.

After investigating, I found that homebrew-cask-versions can be used to manage multiple versions.

The environment I tried

OS: macOS Catalina (version 10.15.4) Terminal: iTerm2

Homebrew installation

Find out if Homebrew is included

$ brew -v

If the version is not displayed, it is installed.

Update Homebrew once installed

$ brew update

If not installed

The command to install is described on Official site. You can install it by copying it, pasting it into the terminal, and executing it.

Described commands as of May 09, 2020

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install homebrew-cask-versions

It is a function that manages multiple versions.

Execute the installation command described on the Official Site. Described commands as of May 09, 2020

$ brew tap homebrew/cask-versions

It's more like a Homebrew extension than an installation.

Java14 installation

The latest version as of May 09, 2020 is Java 14, so without versioning Java 14 will be installed when you install it.

$ brew cask install java

Java11 installation

$ brew cask install java11

Java8 installation

$ brew cask install java8
Error: Cask 'java8' is unavailable: No Cask with this name exists.

-> Error occurs. It seems that an error occurs due to the license.

Install "adoptopenjdk8" which is prepared as an alternative.

$ brew cask install homebrew/cask-versions/adoptopenjdk8

Check installed Java

$ /usr/libexec/java_home -V

Matching Java Virtual Machines (3):
    14.0.1, x86_64:	"OpenJDK 14.0.1"	/Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
    1.8.0_252, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home

Switch Java version

Update the configuration file that is loaded when the shell you are using starts up.

By default, a shell called bash is used, I think the configuration file is a file called "~ / .bashrc".

Add it to the two environment variables "JAVA_HOME" and "PATH".

Please write one of the following three according to the version you want to use.

Java14

export JAVA_HOME=`/usr/libexec/java_home -v "14"`
PATH=${JAVA_HOME}/bin:${PATH}

Java11

export JAVA_HOME=`/usr/libexec/java_home -v "11"`
PATH=${JAVA_HOME}/bin:${PATH}

Java8

export JAVA_HOME=`/usr/libexec/java_home -v "1.8"`
PATH=${JAVA_HOME}/bin:${PATH}

After writing, launch a new terminal application Check the Java version.

$ java --version

Bonus (Visual Studio Code settings)

/usr/libexec/java_home -V

Check the path of each version of Java using

In the Visual Studio Code settings [⌘ +,], If you search for "java home", the configuration file will be opened.

Specify the path of the current Java version in "java.home".

Example:
"java.home": "/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home"

Finally

That's the "how to switch between multiple versions of Java using Homebrew on a Mac" that I researched.

I would be grateful if you could let me know if there are any strange points or better ways.

After the installation, I would like to write a program or debug it.

I also wrote an article about building a development environment in Java (writing a program or executing debug), so please refer to it if you like.

-Java Development Environment (Mac, VS Code) -Java Development Environment (Mac, Eclipse)

end.

Recommended Posts

Java installation (Mac, Homebrew, multiple version control)
Learning Java framework # 1 (Mac version)
Java version control on macOS
Version control Java with SDKMAN
Java version control with jenv
Simplify java version switching (Mac)
Java installation location for mac
java installation
Installation of OMC APM Agent --Java version-
Download and install Eclipse (Java) (Mac version)
# Java: vol1: [java / JDK version control best practice]
Java control syntax
Java control syntax
Install java and android-sdk on Mac using homebrew
Java installation jdk
Java version check
Switch between multiple versions of Java on Mac
Java installation location for mac
Do HelloWorld in Java / IntelliJ / Gradle
Ruby on Rails installation method [Mac edition]
Getting Started with Docker for Mac (Installation)
[Environment construction] Spring Tool Suite4 installation (Mac)
Java installation (Mac, Homebrew, multiple version control)
Java (eclipse) installation procedure
Install Java on Mac
Java version notation comparison
Java string multiple replacement
[Java] Control syntax notes
Switching java version (memorial)
[Rails version control] rails version downgrade
Using multiple versions of Java with Brew on Mac + jEnv
Eclipse installation and code completion enhancements (Mac for Java development)