1. Download and install JDK
-
First enter the official website to download jdk:,
https://www.oracle.com/java/technologies/javase-downloads.html
select the appropriate version to download, for example, the jdk-13.0.2_osx-x64_bin.dmg I downloaded -
Downloading from the official website may be slow, you can download it from the mirror station: jdk-13_osx-x64_bin_jb51.dmg
-
Double-click the dmg file to install
-
Check whether the installation is successful: open Terminal and enter:
java -version
macdeMacBook-Pro:test-java mac$ java -version java version "13-ea" 2019-09-17 Java(TM) SE Runtime Environment (build 13-ea+33) Java HotSpot(TM) 64-Bit Server VM (build 13-ea+33, mixed mode, sharing)
-
JDK introduction (taken from Wikipedia-JDK ):
- Java Development Kit (JDK) is a free software development kit (SDK) released for Java developers . Since the introduction of Java, JDK has become the most widely used Java SDK.
- As a Java language SDK, ordinary users do not need to install JDK to run Java programs, but only need to install JRE (Java Runtime Environment). The program developer must install the JDK to compile and debug the program.
- JDK also includes a complete JRE (Java Runtime Environment), Java runtime environment, also known as private runtime. Class library includes a variety of products for the environment, such as the base class library rt.jar, as well as additional libraries for developers to use, such as internationalization and localization of libraries , IDL library and so on.
- The JDK also includes a variety of sample programs to show each part of the Java API .
2. Configure environment variables
-
Open the configuration file profile:
- Open Terminal and enter:
sudo vim /etc/profile
- Open Terminal and enter:
-
Add the JAVA_HOME path in the configuration file:
-
Switch the English input method, type
i
, and enter the insert mode of vim (Vim related commands can be searched and learned by yourself) -
Add the following configuration at the end of the article:
JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/" CLASS_PATH="$JAVA_HOME/lib" PATH=".:$PATH:$JAVA_HOME/bin"
- Then click "esc" to exit insert mode, enter to
:wq!
save and exit
- Then click "esc" to exit insert mode, enter to
-
The first line is the default installation directory of jdk. Specific viewing method: open Finder, shortcut key "Command + Shift + G", enter and press
/Library/Java/JavaVirtualMachines/
Enter to see -
The second line CLASS_PATH is to enable the class file to run in any directory
-
The third line PATH is to enable javac to run in any directory
-
-
After adding the above configuration, my profile content is as follows:
1 # System-wide .profile for sh(1) 2 3 if [ -x /usr/libexec/path_helper ]; then 4 eval `/usr/libexec/path_helper -s` 5 fi 6 7 if [ "${BASH-no}" != "no" ]; then 8 [ -r /etc/bashrc ] && . /etc/bashrc 9 fi 10 11 JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/" 12 13 CLASS_PATH="$JAVA_HOME/lib" 14 15 PATH=".:$PATH:$JAVA_HOME/bin"
-
Make the configuration file profile effective: restart Terminal, or enter:
source /etc/profile
-
Check whether the configuration takes effect:
- Open Terminal and enter:
echo $JAVA_HOME
macdeMacBook-Pro:test-java mac$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/
- Open Terminal and enter:
3. Write, compile and run source code
-
Create a folder:
-
Open Terminal, enter
mkdir java-test
-
Enter the folder: enter
cd java-test
-
-
Create java source files:
-
enter
vim HelloWorld.java
-
Switch the English input method, type
i
, enter the insert mode of vim, and then paste the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
- Then click "esc" to exit insert mode, enter
:wq!
, save and exit
-
-
Compile and run:
-
Open Terminal, enter the
javac HelloWorld.java
command, compile the "HelloWorld.java" source code file, and compile it into a binary bytecode file (end with ".class") -
Enter the
java HelloWorld
command to interpret the "HelloWorld.class" bytecode file (through interpreters of different platforms), and then we can see the results of the program running:macdeMacBook-Pro:Desktop mac$ cd java-test macdeMacBook-Pro:java-test mac$ ls macdeMacBook-Pro:java-test mac$ vim HelloWorld.java macdeMacBook-Pro:java-test mac$ ls HelloWorld.java macdeMacBook-Pro:java-test mac$ javac HelloWorld.java macdeMacBook-Pro:java-test mac$ ls HelloWorld.class HelloWorld.java macdeMacBook-Pro:java-test mac$ java HelloWorld Hello World! macdeMacBook-Pro:java-test mac$
-
Reference link: