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)
A language used for various developments such as large-scale systems (accounting systems of financial institutions) and applications for mobile terminals.
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.
- Create a source file written according to the rules set in the Java language. ↓ Compile (javac command = java compiler)
- 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)
- 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 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.
- Independent (encapsulated) A mechanism that prevents interference with other programs as much as possible.
- 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.
- 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.
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
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!
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! ``
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! ``
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! ``
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