Install Java development environment on Mac

last updated

June 1, 2020

The verified macOS version is as follows.

The version to be installed is as follows.

If this article is out of date, the steps below may differ from the latest installation steps.

JDK

What is JDK

Abbreviation for Java Development Kit. The compiler and JVM (Java Virtual Machine) required for Java development are integrated.

The JDK is provided by various organizations and vendors. This time we will use AdoptOpenJDK.

If you want to install multiple versions of the JDK, you should use SDKMAN!.

JDK installation

(1) Please access https://adoptopenjdk.net.

(2) After selecting [OpenJDK 11] and [HotSpot], click [Latest Release].

スクリーンショット 2020-06-01 9.32.06.png

(3) Click [Allow].

スクリーンショット 2020-06-01 9.36.13.png

(4) Execute the downloaded .pkg file.

スクリーンショット 2020-06-01 9.37.03.png

(5) Click [Continue].

スクリーンショット 2020-06-01 9.37.58.png

(6) Click [Continue].

スクリーンショット 2020-06-01 9.38.45.png

(7) Click [Agree].

スクリーンショット 2020-06-01 9.39.00.png

(8) Click [Install].

スクリーンショット 2020-06-01 9.39.21.png

(9) (1) Use Touch ID, or (2) Click [Use Password ...] and enter the password for your Mac account.

スクリーンショット 2020-06-01 9.39.43.png

(10) Click [Close].

スクリーンショット 2020-06-01 9.43.40.png

(11) Execute the following command in the terminal (start it if it is not started). This sets the environment variable JAVA_HOME.

cd ~
echo JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home >> .zshrc
source .zshrc
echo $JAVA_HOME

The first command, cd ~, takes you to the user home folder (/ Users / username).

The second command ʻecho JAVA_HOME = / Library / Java / JavaVirtualMachines / adoptopenjdk-11.jdk / Contents / Home >> .zshrcin the .zshrc file (created if it does not exist) directly under the user home folder The contentJAVA_HOME = / Library / Java / JavaVirtualMachines / adoptopenjdk-11.jdk / Contents / Home` is added to the last line.

The command written in this .zshrc file is executed when the terminal etc. is started.

The third command, source .zshrc, runs the command written in .zshrc. If you restart the terminal after running the second command, you don't need this third command.

The fourth command displays the value of the environment variable JAVA_HOME. If it is set correctly, the set folder name will be displayed. (If it is not set, nothing is displayed)

If you execute these four commands in the terminal, it will look like this.

スクリーンショット 2020-06-01 11.48.39.png

This environment variable JAVA_HOME is used by various Java related tools as the" installation destination of the JDK ". If you do not set it, those tools may not work.

(12) Execute the following command in the terminal. This sets the environment variable PATH.

cd ~
echo PATH=$PATH:$JAVA_HOME/bin >> .zshrc
source .zshrc
echo $PATH

The first command, cd ~, takes you to the user home folder (/ Users / username). If you are already in your user home folder, you don't need this command.

The second command, ʻecho PATH = $ PATH: $ JAVA_HOME / bin >> .zshrc, puts PATH = $ PATH: on the last line of the .zshrc file (created if it doesn't exist) directly under the user home folder. The contents of $ JAVA_HOME / bin` are added.

The third command, source .zshrc, runs the command written in .zshrc. If you restart the terminal after running the second command, you don't need this third command.

The fourth command displays the value of the environment variable PATH. If set correctly, the display will include /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin at the end.

If you execute these four commands in the terminal, it will look like this.

スクリーンショット 2020-06-01 11.50.55.png

This environment variable PATH is the" folder where the terminal etc. looks for the command to execute ". For Java, commands such as java and javac are located in the / Library / Java / JavaVirtualMachines / adoptopenjdk-11.jdk / Contents / Home / bin folder, so this setting is required.

(13) Execute the following command in the terminal. This will allow you to see the version of the JDK installed.

java --version
javac --version

If you execute these two commands in the terminal, it will look like this.

スクリーンショット 2020-06-01 10.38.57.png

(14) Close the terminal.

If the installation doesn't work

If you run ls / Library / Java / JavaVirtualMachines and nothing is displayed

The JDK has not been installed, so start over from (4).

If you run ʻecho $ JAVA_HOME` and nothing is displayed or a different folder is displayed

Failed to set the environment variable JAVA_HOME.

Open the .zshrc file with VSCode etc. and modify it, or delete .zshrc with rm -f ~ / .zshrc and start over from (10).

ʻEcho $ PATH does not show / Library / Java / JavaVirtualMachines / adoptopenjdk-11.jdk / Contents / Home / bin` at the end

Failed to set the environment variable PATH.

Open the .zshrc file with VSCode etc. and modify it, or delete .zshrc with rm -f ~ / .zshrc and start over from (10).

IntelliJ IDEA

What is IntelliJ IDEA

It is an IDE (Integrated Development Environment) developed by JetBrains of the Czech Republic. It is very popular because it is highly functional and easy to operate.

Other IDEs for Java include Eclipse and NetBeans.

There is a free Community Edition and a paid Ultimate Edition (available as an evaluation version for 30 days free of charge).

This time, I will show you how to install Community Edition.

The Ultimate Edition installation should be pretty much the same. If you would like to purchase it, please visit the Samuraism website.

Install IntelliJ IDEA Community Edition

If you want to install multiple versions of IntelliJ IDEA, you can use JetBrains Toolbox.

(1) Please access https://www.jetbrains.com/ja-jp/idea/download/.

(2) Click the [Download] button in [Community].

スクリーンショット 2020-06-01 10.55.52.png

(3) Click [Allow].

スクリーンショット 2020-06-01 10.56.24.png

(4) Execute the downloaded .dmg file.

スクリーンショット 2020-06-01 10.57.24.png

(5) Drag the [IntelliJ IDEA CE] icon to the [Applications] folder on the right side.

スクリーンショット 2020-06-01 10.57.54.png

(6) Click the red circle on the upper left. The window closes.

(7) Right-click [IntelliJ IDEA CE] on the desktop → select [Eject "IntelliJ IDEA CE"].

スクリーンショット 2020-06-01 10.59.53.png

(8) Open Launchpad and add [IntelliJ IDEA CE] to the Dock.

スクリーンショット 2020-06-01 11.01.18.png

(9) Start [IntelliJ IDEA CE] from the Dock.

スクリーンショット 2020-06-01 11.02.21.png

(10) Select [Open].

スクリーンショット 2020-06-01 11.02.49.png

(11) Check [I confirm that I have read and accept the terms of this User Agreement], and then click [Continue].

スクリーンショット 2020-06-01 11.03.11.png

(12) You will be asked if you want to share your statistics anonymously with JetBrains. Click Send Anonymous Statistics if you want to share it, or Don't Send if you don't.

スクリーンショット 2020-06-01 11.04.32.png

(13) Select [Configure]-[Structure for New Projects].

スクリーンショット 2020-06-01 11.07.29.png

(14) Select [11 java version 11.0.7] in [Project SDK], [SDK default] in [Project language level], and then click [OK].

スクリーンショット 2020-06-01 11.08.40.png

Set IntelliJ IDEA to your liking (steps here are not required)

(1) Select [Configure]-[Preferences].

スクリーンショット 2020-06-01 11.11.03.png

(2) Select [Appearance & Behavior]-[Appearance] and select your favorite color theme in [Theme].

スクリーンショット 2020-06-01 11.12.39.png

(3) Select [Editor]-[Font] and select your favorite [Font] / [Size].

スクリーンショット 2020-06-01 13.07.46.png

(4) Select [Editor]-[General]-[Code Completion] and uncheck [Match case].

スクリーンショット 2020-06-01 11.16.58.png

The default setting (Match case + First letter only) does not complete unless you write the case exactly. If unchecked, it will be completed ignoring case.

(5) Click [OK] at the bottom right of the window.

(6) Select [Configure]-[Edit Custom VM Options ...].

スクリーンショット 2020-06-01 11.21.17.png

You can change the amount of memory used by IntelliJ IDEA by rewriting the lines -Xms and -XmX in the window that opens. If you have enough memory on your Mac, you can increase it to 1G or 2G to make Intellij IDEA work lighter. Click [Save] after making changes.

Example: スクリーンショット 2020-06-01 11.23.33.png

Hello World in IntelliJ IDEA

(1) Click [Create New Project].

スクリーンショット 2020-06-01 11.27.06.png

(2) Select [Java] in the left menu, [11] in [Project SDK], and then click [Next].

スクリーンショット 2020-06-01 11.27.55.png

(3) Click [Next] as it is.

スクリーンショット 2020-06-01 11.28.54.png

(4) Enter an appropriate project name in [Project name], and then click [Finish].

スクリーンショット 2020-06-01 11.29.19.png

(5) [Tip of the Day] will tell you the tips on how to use it every time you open IntelliJ IDEA. If you do not need it, check [Don't show tips] and then click [Close]. Click Close if necessary.

スクリーンショット 2020-06-01 11.30.14.png

(6) Right-click the [src] folder and select [New]-[Java Class].

スクリーンショット 2020-06-01 11.32.31.png

You can do the same with Command + N after hovering over the [src] folder.

スクリーンショット 2020-06-01 11.33.40.png

(7) After selecting [Class], enter "Hello" → Enter.

スクリーンショット 2020-06-01 11.34.04.png

(8) Edit the Hello class as follows (it is not necessary because saving is done automatically).

public class Hello {

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

Type psvm directly under the class → select psvm and press Enter to complete the main () method.

スクリーンショット 2020-06-01 11.36.39.png

Type sout in themain ()method → select sout and press Enter to complete System.out.println ().

スクリーンショット 2020-06-01 11.38.46.png

(9) Right-click in the class and select [Run'Hello.main ()'] to execute.

スクリーンショット 2020-06-01 11.39.52.png

It can also be executed by Shift + Control + R in the class, but it will not be executed well unless the keyboard setting on the macOS side is [U.S.]. For details, please see @ yusuke's material.

Maven

What is Maven

It's commonly called a "build tool". It compiles using javac, archives to JAR / WAR, downloads necessary libraries + adds them to the classpath, etc.

For an explanation about Maven, please see My Material!

Besides Maven, Gradle is also famous.

Maven installation

You can also install with SDKMAN! mentioned above or Homebrew.

(1) Please access https://maven.apache.org/download.cgi.

(2) Click [apache-maven-3.6.3-bin.zip].

スクリーンショット 2020-06-01 13.29.16.png

(3) Click [Allow].

(4) Double-click the downloaded ZIP file. The ZIP will be unzipped.

Depending on your browser settings, it may have already been extracted immediately after downloading. In that case, this step is not necessary.

(5) Move the created apache-maven-3.6.3 folder directly under the user home folder (/ Users / user name).

You can use it in the Downloads folder, but move it to another folder so that you don't accidentally delete it.

(6) Execute the following command in the terminal. This sets the environment variable PATH.

cd ~
echo PATH=$PATH:~/apache-maven-3.6.3/bin >> .zshrc
source .zshrc
echo $PATH

The first command, cd ~, takes you to the user home folder (/ Users / username). If you are already in your user home folder, you don't need this command.

The second command, ʻecho PATH = $ PATH: ~ / apache-maven-3.6.3 / bin >> .zshrc, puts PATH = $ PATH: ~ / on the last line of the .zshrc file directly under the user home folder. The contents of apache-maven-3.6.3 / bin` are added.

The third command, source .zshrc, runs the command written in .zshrc. If you restart the terminal after running the second command, you don't need this third command.

The fourth command displays the value of the environment variable PATH. If set correctly, the display will include / Users / username / apache-maven-3.6.3 / bin at the end.

If you execute these four commands in the terminal, it will look like this.

スクリーンショット 2020-06-01 13.47.13.png

(7) Execute the following command in the terminal. This will allow you to see which version of Maven you have installed.

mvn -v

When I run this command in the terminal, it looks like this:

スクリーンショット 2020-06-01 13.48.19.png

Recommended Posts

Install Java development environment on Mac
Install Java on Mac
Java development environment construction memo on Mac
Build a Java development environment on Mac
Java development environment (Mac, Eclipse)
Java development environment
Build Java development environment (for Mac)
Java development environment (Mac, VS Code)
What is Java and Development Environment (MAC)
Build Java 8 development environment on AWS Cloud9
Install gradle on mac
Java development environment memo
java development environment construction
Install openjdk11 on mac
Install OpenJDK 8 on mac
[ev3 × Java] leJOS development environment construction (Eclipse on Mac OSX / bluetooth)
Install java and android-sdk on Mac using homebrew
[Java] Build Java development environment on Ubuntu & check execution
Rails6 development environment construction [Mac]
Install OpenJDK7 (JAVA) on ubuntu 14.04
Install Java on WSL Ubuntu 18.04
Install tomcat + eclipse on mac
Install java 1.8.0 on Amazon linux2
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Install java and maven using brew on new mac
Install Java / Tomcat / PostgreSQL without polluting your Mac environment
[Eclipse Java] Development environment setting memo
Prepare Java development environment with Atom
Play Framework 2.6 (Java) development environment creation
Install Java Open JDK 8 on CentOS 7
About the current development environment (Java 8)
Learn the meaning of "passing the PATH" by building a Java development environment on Mac
[For beginners] Until building a Web application development environment using Java on Mac OS
[Java & SpringBoot] Environment Construction for Mac
Install Java 9 on windows 10 and CentOS 7
Build Unity development environment on docker
I tried to create a Spring MVC development environment on Mac
Notes on building Kotlin development environment and migrating from Java to Kotlin
How to check Java installed on Mac
If you can't install java on Catalina
Prepare Java development environment with VS Code
Create Spring Boot development environment on Vagrant
[Processing x Java] Construction of development environment
Spring Boot environment construction memo on mac
Laravel development environment construction with Docker (Mac)
How to install Eclipse (Photon) on Mac
I can't install rails on my mac
Let's create a Java development environment (updating)
[Ruby] Building a Ruby development environment on Ubuntu
Spring Boot + Docker Java development environment construction
Install Java8 with Yum on Amazon Linux
Hello World on Mac VS Code Java
Download and install Eclipse (Java) (Mac version)
Run jooby's Eclipse development environment on Gradle
[Personal memo] Java development environment is ready
Build a JMeter environment on your Mac
[Mac] Install Java in Visual Studio Code
Build an Ultra96v2 development environment on Docker 1
Java application development environment created in VM environment
Install Docker and create Java runtime environment
Java development training