1. Introduction
Begin your Java programming journey with our comprehensive JDK installation guide. Whether on macOS, Linux, or Windows, this beginner-friendly walkthrough ensures a seamless setup of your development environment with the Java Development Kit (JDK).
2. What is a JDK?
As we saw in the previous article on the difference between JRE, JDK, and JVM, the JDK is the essential tool for getting started with Java programming. It contains everything you need to compile and run your Java code. Before starting with this JDK installation guide, let’s have a quick look at the Java Platform, Standard Edition (Java SE).
3. What do you need to know?
Java Platform, Standard Edition (Java SE) is a specification that outlines the fundamental tools and libraries necessary for Java application development. It defines a set of standards that ensure compatibility across various implementations.
Multiple organizations provide their implementations of Java SE, each offering a version of the platform that adheres to the specified standards. However, all JDK implementations are based on the OpenJDK reference implementation, which is an open-source project.
These are the two JDK implementations you’re most likely to hear about :
- OpenJDK: The official reference implementation of the Java Platform, Standard Edition (Java SE). It is an open-source project, and many other JDK builds are based on the OpenJDK codebase.
- Oracle JDK: Provided by Oracle, it is based on the OpenJDK codebase and includes additional features, tools, and commercial support. Oracle JDK has traditionally been one of the most widely used JDKs.
For all our articles, we’ll be working exclusively with OpenJDK.
4. Check the current version of JDK
Before proceeding with the installation, please check what JDK version is currently installed on your computer.
Enter the following commands from a command prompt or terminal (depending on your OS):
javac -version
If you don’t have any JDK installed, you should have an output similar to this one:
-bash: javac: command not found
You can also check the version of the JRE installed with the following command:
java -version
As with the JDK, you’ll get an output similar to this:
-bash: java: command not found
If you don’t get the same results as shown here, you may already have another version of JDK (JRE) installed. This is not a problem, as you’ll modify your system to indicate which JDK version to use.
5. Download the JDK
Oracle’s OpenJDK JDK binaries for Windows, macOS, and Linux are available on release-specific pages of jdk.java.net as .tar.gz or .zip archives.
You will be installing OpenJDK 17. By the time of writing this article, you can find it here.
Just download the archive file corresponding to your Operating System.
6. Install the JDK on MacOC
6.1. Extract the Downloaded archive
Create an apps directory at the root of your user directory. Copy/Paste the downloaded archive into that directory.
Then run the following commands from the terminal:
cd ~
cd apps
tar xvf openjdk-17*_bin.tar.gz
6.2. Update the environment variables PATH and JAVA_HOME
From the terminal, open your Shell profile:
vi ~/.bash_profile
Update your Shell profile by adding the following lines at the beginning of the file:
export JAVA_HOME=/Users/user01/apps/jdk-17.0.1.jdk
export PATH=$JAVA_HOME/Contents/Home/bin/:$PATH
Replace “user01” with your username and “jdk-17.0.1.jdk” to match your downloaded JDK version.
Source your profile file to see the changes:
source ~/.bash_profile
You can simply restart the terminal to see the changes as well.
6.3. Check the new JDK version
Run the following command to test your installation:
Check the Compiler(JDK) version
javac -version
You will see this in your console:
javac 17.0.7
Check the Runtime(JRE) version
java -version
Output:
java version "17.0.7" 2023-04-18 LTS
Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)
7. Install the JDK on Linux
7.1. Extract the Downloaded Archive
Create an apps directory at the root of your user directory. Copy/Paste the downloaded archive into this directory.
Then run the following commands from the terminal:
cd ~
cd apps
tar xvf openjdk-17*_bin.tar.gz
7.2. Update the environment variables PATH and JAVA_HOME
From the terminal, open your Shell profile:
vi ~/.bashrc
Update your Shell profile by adding the following lines at the beginning of the file:
export JAVA_HOME=/home/user01/apps/jdk-17.0.1.jdk
export PATH=$JAVA_HOME/bin/:$PATH
Replace “user01” with your username and “jdk-17.0.1.jdk” to match your downloaded JDK version.
Source your profile file to see the changes:
source ~/.bashrc
You can simply restart the terminal to see the changes as well.
7.3. Check the new JDK version
Run the following commands to test your installation.
Check the Compiler(JDK) version
javac -version
You will see this in your console:
javac 17.0.7
Check the Runtime(JRE) version
java -version
Output:
java version "17.0.7" 2023-04-18 LTS
Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)
8. Install the JDK on Windows
8.1. Extract the Downloaded Archive
Create an apps directory at the root directory of drive C. Copy/Paste the downloaded archive into this directory.
Then run the following commands from the command line:
cd C:\apps
unzip openjdk-17*_bin.zip
8.2. Update the environment variables PATH and JAVA_HOME
Create a user environment variable named JAVA_HOME with the following value: C:\apps\jdk-17.0.1.jdk
Update the PATH environment variable by adding the following value: %JAVA_HOME%\bin
Replace “jdk-17.0.1.jdk” to match the JDK version you downloaded.
8.3. Check the new JDK version
Run the following commands to test your installation.
Check the Compiler(JDK) version
javac -version
You will see this in your console:
javac 17.0.7
Check the Runtime(JRE) version
java -version
Output:
java version "17.0.7" 2023-04-18 LTS
Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)
9. Conclusion
Congratulations on completing the JDK installation guide! In the upcoming article, embark on the exciting journey of crafting your first Java project with our step-by-step tutorial.
If you have any difficulty completing this tutorial, leave a comment, and I’ll be happy to assist you.
Pingback: Access Modifiers in Java: Gain a Comprehensive Understanding
Pingback: Object-Oriented in Java: Building Robust Applications
Pingback: Inheritance in Java: Building on Basics for Powerful Programming
Pingback: Java Hello World Program
Pingback: Debugging Java Applications: A Guide with IntelliJ IDEA
Pingback: Records In Java
Pingback: Sealed Classes and Interfaces In Java