Open In App

How to Install Java on Windows, Linux and macOS?

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Java is a versatile programming language widely used for building applications. To start coding in Java, you first need to install the Java Development Kit (JDK) on your system. This article provides detailed steps for installing Java on Windows 7, 8, 10, 11, Linux Ubuntu, and macOS.

Download and Install Java – Windows Operating System

Here, we will discuss how to download and install Java on a Windows 64-bit Machine and set up the environment to run the first Java program on the command prompt. The process is the same for all Windows i.e. 7, 8, 10, and 11.

Step 1: Visit the official Oracle website https://www.oracle.com/in/java/. Click on the “Download Java” icon as shown in the below image.

Install-Download-Java-1

Step 2: Now you can see the latest version is JDK 23 and there are options for Linux, macOS, and Windows. Click on the Windows option and then click the x64 Installer option to download the .exe file for 64-bit Windows OS.

Install-Download-Java-2

You can simply use this link to download Java for Windows 64 Bit. After successfully downloading JDK 23, now we need to install the Java on our Windows System.

Step 3: Go to Downloads and double-click on that downloaded jdk-23_windows-x64_bin.exe file. So now Java Installation Wizard get open and then click Next button.

Install-Download-Java-3

Again click on the Next button to install JDK.

Install-Download-Java-4

The installation will begin as shown below:

Install-Download-Java-5

The Java installation is successfully completed as shown below. Now click on “close” to finish.

Install-Download-Java-6

Step 4: Now we will set the environment variables for Windows OS. Open the C drive, go to Program Files > Java > jdk-23 (or your installed version) > bin folder. Now, copy the path and we will use this path when configuring the environment variables.

Set-Environment-Variables-Windows-1

Search for environment variable on your system, then click on the Environment Variables button.

Set-Environment-Variables-Windows-2

In the system variable section, select the path variable and then click the option Edit.

Set-Environment-Variables---Windows-3

Paste the path that copied earlier, and click OK to save the changes.

Set-Environment-Variables-Windows-4

Step 5: Verify the Installation. Open command prompt and type the below command to verify the Java version that is installed in the system.

java –version

Verify-Installation---Windows

Now let us run our first Java program, to do this, follow the below steps:

Step 1: Open any Drive, and in the drive, create a text file and then change the file name with .java extension as shown below:

file.java

Step 2: Now, write a simple Java code in notepad.

  • Right-click on the file.java and then select open with > Notepad.
  • Inside the file, write the Java code.
Run-Java-Code-1

Important Point: In Java, the class name should be equivalent to the file name. In this case, the file name is file.java, so the class name inside the file should be file.

Step 3: Now, go to the directory where your Java file is saved.

Open command prompt and navigate to the D drive, where the file.java is located. To do this, type “dir” as shown below:

Run-Java-Code-2

Step 4: The dir command display all the contents of the directory.

Run-Java-Code-3

Step 5: Compile the Java program. The javac command is used to compile the Java program.

Syntax:

javac filename.java

To run the program use the below command:

java filename

Here, in this case the filename is file.

Run-Java-Code-4

We have successfully run our first Java program on Windows OS.

Download and Install Java – Linux

Step 1: Visit the official oracle website https://www.oracle.com/in/java/.

  • You will need to accept the terms of Oracle’s license agreement before downloading.
  • On the download page, accept the Oracle Binary Code License Agreement for Java SE.
  • Select the Linux version.
  • If you are using a standard 64-bit system, select the file for x64 architecture. If you are using an ARM-based system, select the file for arm64.
  • Download the .tar.gz file for Linux.

This is the format used for installing Java on most Linux systems.

Step 2: Now, open the terminal on your Linux system and navigate to the folder where you downloaded the file.

cd ~/Downloads

Use the below command to extract the contents of the .tar.gz file.

tar -xvzf jdk-23_linux-x64_bin.tar.gz

Move the extracted folder to the /opt directory:

sudo mv jdk-23 /opt/

Step 3: After Installation, you need to set up the environment variable for Linux.

Open the terminal and type the below command to open the shell configuration file.

nano ~/.bashrc

Add the following lines at the end of the file.

export JAVA_HOME=/opt/jdk-23

export PATH=$JAVA_HOME/bin:$PATH

Apply the changes by running the below command:

source ~/.bashrc

Step 4: Now, verify the installation on Linux by running the below command:

java –version

The installed Java version on Linux will be displayed.

Download and Install Java – macOS

Step 1: Visit the official oracle website https://www.oracle.com/in/java/. Select the macOS version and download the .dmg file.

Step 2: Now open the .dmg file and double-click the installer package (.pkg) to begin the installation process. Then follow the instructions to complete the installation.

Step 3: Setting the environment variable on macOS is optional, but it is recommended, if you want to use Java for development purposes (e.g., running Java-based applications, using tools like Maven or Gradle).

To set the environment variable foe macOS, open the Terminal and edit the shell configuration file (e.g., .zshrc):

nano ~/.zshrc

Add the following lines at the end of the file to set the Java_Home.

export JAVA_HOME=$(/usr/libexec/java_home)

export PATH=$JAVA_HOME/bin:$PATH

Save and close. To apply the changes, run the following command:

source ~/.zshrc

After these steps, Java should be installed and ready to use on your macOS system.

Step 4: Now open the terminal and run the below command to verify the Java version on macOS:

java –version

You will see an output showing the latest Java version that you have installed on your macOS system.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg