[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.

Purpose

I just started learning Java and want to get an overview and build a local development environment.

OS: macOS Mojave version 10.14.6 Text editor: Visual Studio Code (hereinafter VSCode)

Java overview

A language used for various developments such as large-scale systems (accounting systems of financial institutions) and applications for mobile terminals.

① Write Once, Run Anywhere (write once, run anywhere)

Java programs do not run directly on the OS, but run on an execution environment called the JVM (Java Virtual Machine). `` Therefore, there is no dependency with ʻOS, and you do not have to worry about the difference between OS such as Windows and Mac when creating a program.

The flow of program creation and execution using the Java language is as follows.

  1. Create a source file written according to the rules set in the Java language. ↓ Compile (javac command = java compiler)
  2. Become a class file. (A file that can be interpreted by the JVM is generated) Class files, which are intermediate files, are described in executable binaries called bytecode. ↓ Execute (java command)
  3. Run on the JVM The bytecode is interpreted and executed by the JVM installed in the OS.

By installing OpenJDK, which will be described later, you will be able to use javac commands and java commands, and the program will be executed.

② Object-oriented

Object-oriented is a concept created to solve these various problems in large-scale development.

One program is created by distinguishing the functions and roles of the program and combining each as a part.

In principle, there are the following.

  1. Independent (encapsulated) A mechanism that prevents interference with other programs as much as possible.
  2. Highly reusable (inheritance) The parts that behave in the same way can be grouped together (parent class), and the grouped items can be reused (child class) to reduce code duplication.
  3. Easy to expand (polymorphism) While the parts that behave the same are standardized, the parts that you want to behave differently can be changed according to the purpose.

Construction of development environment

The Java development environment is built using the JDK (Java SE Development Kit). There are various types such as Oracle JDK and OpenJDK.

This time, in order to use the JDK, install the following in the terminal. ・ Homebrew ・ Homebrew-cask ・ Homebrew-cask-versions ・ OpenJDK 11

Homebrew installation

From the installation of Homebrew, the package manager for macOS. With `Homebrew, you can install and uninstall open source software from the terminal. ``

** Homebrew Official **

Terminal


$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following existing directories will be made group writable:
/usr/local/bin
/usr/local/include
/usr/local/lib
/usr/local/share
/usr/local/share/doc
/usr/local/share/man
~ The following is omitted ~

During the installation, you will be asked for your PC password twice, so enter it.

Terminal


$ brew --version
Homebrew 2.2.15
Homebrew/homebrew-core (git revision aafb6c8; last commit 2020-05-08)

Homebrew installation complete!

Install homebrew-cask

Next, install homebrew-cask, which is an extension command of Homebrew. `You will be able to install the GUI app from the terminal. ``

Download the app in your browser, unzip the dmg file in the download folder, Move to the Application folder and ... is no longer necessary.

** Homebrew-cask GitHub **

Terminal


$ brew cask
==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 437237 (delta 2), reused 0 (delta 0), pack-reused 437230
Receiving objects: 100% (437237/437237), 196.54 MiB | 79.00 KiB/s, done.
Resolving deltas: 100% (309524/309524), done.
~ The following is omitted ~

You will not be asked for your password this time.

Terminal


$ brew --version
Homebrew 2.2.15
Homebrew/homebrew-core (git revision aafb6c8; last commit 2020-05-08)
Homebrew/homebrew-cask (git revision 9fc819; last commit 2020-05-08)

`Homebrew-cask installation complete! ``

Install homebrew-cask-versions

It is possible to specify the version and install it. (Multiple version control will be possible)

** GitHub for homebrew-cask-versions **

Terminal


$ brew tap homebrew/cask-versions
Updating Homebrew...
==> Tapping homebrew/cask-versions
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions'...
remote: Enumerating objects: 73, done.
remote: Counting objects: 100% (73/73), done.
remote: Compressing objects: 100% (61/61), done.
remote: Total 225951 (delta 39), reused 19 (delta 12), pack-reused 225878
Receiving objects: 100% (225951/225951), 58.32 MiB | 79.00 KiB/s, done.
Resolving deltas: 100% (155423/155423), done.
Tapped 151 casks (198 files, 64.6MB).

You will not be asked for your password this time either.

Installation of `homebrew-cask-versions is complete! ``

Installation of OpenJDK 11

Finally, install OpenJDK 11. Check the current Java version.

Terminal


$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

Then enter the following command.

Terminal


$ brew cask install java11
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
aws-cdk                 gitlab-runner
==> Downloading https://download.oracle.com/java/GA/jdk11/9/GPL/openjdk-11.0.2_o
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'java11'.
==> Installing Cask java11
==> Moving Generic Artifact 'jdk-11.0.2.jdk' to '/Library/Java/JavaVirtualMachin
Password:
java11 was successfully installed!

This time, you will be asked for the password only once, so enter it.

Terminal


$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

$ javac -version
javac 11.0.2

ʻOpenJDK 11 installation completed! ``

Try to compile → execute

Create a Java working folder and open VS Code. Create a file called Hello.java in that folder.

java_practice   └ Hello.java

This time, let the terminal output Hello World.

Hello.java


class Hello {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

As explained at the beginning Compile the source file with the javac command to create a class file.

Terminal


$ javac Hello.java
$ ls
Hello.java     Hello.class ← Created!

java_practice   ├ Hello.java └ Hello.class ← Created!

Execute the class file with the java command and output Hello World. Do not add the `.class extension. ``

Terminal


$ java Hello
Hello World ← Output!

It was executed successfully. Thank you for your hard work!

Recommended Posts

[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.
Install Java 11 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install Java 14 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install Java 8 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install Java 8 (OpenJDK: Amazon Corretto) on macOS with Homebrew
Install Java 8 (OpenJDK: Zulu Community) on macOS with Homebrew
Java development environment construction on Mac-JDK Install (2020 preservation version)
Install Java development environment on Mac
Java development environment construction memo on Mac
Install java with Homebrew
Install OpenJDK on macOS
java development environment construction
Install OpenJDK7 (JAVA) on ubuntu 14.04
Install Java 7 with Homebrew (cask)
[Environment construction] Build a Java development environment with VS Code!
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Install Java with zip on Windows
Prepare Java development environment with Atom
Java web application development environment construction with VS Code (struts2)
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
[ev3 × Java] leJOS development environment construction (Eclipse on Mac OSX / bluetooth)
Prepare Java development environment with VS Code
[Processing x Java] Construction of development environment
Laravel development environment construction with Docker (Mac)
Install OpenJDK (Java) on the latest Ubuntu
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
Spring Boot + Docker Java development environment construction
Install Java8 with Yum on Amazon Linux
[Java] Environment construction
Java development environment
[Jakarta EE 8 application development with Gradle] 1. Environment construction
Easily switch Java versions with alias on macOS
Environment construction command memo with Docker on AWS
[Java] Build Java development environment on Ubuntu & check execution
Ruby on Rails development environment construction on M1 Mac
[Beginner] Install java development tool in cloud9 development environment.
GitLab development environment setup (GDK) on macOS (September 2020)
Build a Java development environment with VS Code
Java development environment memo
Install Java on Mac
Install Java with Ansible
Install OpenJDK8 with RPM
Install openjdk11 on mac
Install Homebrew on Ubuntu 20.04
Install OpenJDK 8 on mac
Create a Java development environment using jenv on Mac
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Stable development environment construction manual for "Rails6" with "Docker-compose"
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
GOOS book Openfire On MacOS Mojave environment construction memo
Install Ruby 3.0.0-preview1 on macOS (also without Homebrew ruby-build)
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)
Java version control on macOS
Rails6 development environment construction [Mac]
[Spring Boot] Environment construction (macOS)
I tried to create a java8 development environment with Chocolatey
Web application development environment construction in Java (for inexperienced people)