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
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!.
(1) Please access https://adoptopenjdk.net.
(2) After selecting [OpenJDK 11] and [HotSpot], click [Latest Release].
(3) Click [Allow].
(4) Execute the downloaded .pkg file.
(5) Click [Continue].
(6) Click [Continue].
(7) Click [Agree].
(8) Click [Install].
(9) (1) Use Touch ID, or (2) Click [Use Password ...] and enter the password for your Mac account.
(10) Click [Close].
(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 content
JAVA_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.
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.
This environment variable
PATH
is the" folder where the terminal etc. looks for the command to execute ". For Java, commands such asjava
andjavac
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.
(14) Close the terminal.
ls / Library / Java / JavaVirtualMachines
and nothing is displayedThe JDK has not been installed, so start over from (4).
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).
does not show
/ Library / Java / JavaVirtualMachines / adoptopenjdk-11.jdk / Contents / Home / bin` at the endFailed 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
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.
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].
(3) Click [Allow].
(4) Execute the downloaded .dmg file.
(5) Drag the [IntelliJ IDEA CE] icon to the [Applications] folder on the right side.
(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"].
(8) Open Launchpad and add [IntelliJ IDEA CE] to the Dock.
(9) Start [IntelliJ IDEA CE] from the Dock.
(10) Select [Open].
(11) Check [I confirm that I have read and accept the terms of this User Agreement], and then click [Continue].
(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.
(13) Select [Configure]-[Structure for New Projects].
(14) Select [11 java version 11.0.7] in [Project SDK], [SDK default] in [Project language level], and then click [OK].
(1) Select [Configure]-[Preferences].
(2) Select [Appearance & Behavior]-[Appearance] and select your favorite color theme in [Theme].
(3) Select [Editor]-[Font] and select your favorite [Font] / [Size].
(4) Select [Editor]-[General]-[Code Completion] and uncheck [Match case].
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 ...].
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:
(1) Click [Create New Project].
(2) Select [Java] in the left menu, [11] in [Project SDK], and then click [Next].
(3) Click [Next] as it is.
(4) Enter an appropriate project name in [Project name], and then click [Finish].
(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.
(6) Right-click the [src] folder and select [New]-[Java Class].
You can do the same with Command + N after hovering over the [src] folder.
(7) After selecting [Class], enter "Hello" → Enter.
(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 → selectpsvm
and press Enter to complete themain ()
method.
Type
sout
in themain ()
method → selectsout
and press Enter to completeSystem.out.println ()
.
(9) Right-click in the class and select [Run'Hello.main ()'] to execute.
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
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.
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].
(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.
(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:
Recommended Posts