Installing and Setting Up Java

Author - Venkat

About this article - A Beginner's Guide to Installing and Setting Up Java.


Setting up Java can seem a little tricky, but don’t worry! This guide will walk you through each step in simple terms. By the end, you'll have Java ready to use on your computer.

Step 1: Download Java

  1. Visit the Java Download Page:
    Go to the Oracle Java SE Downloads page.
    If you want a free and open-source option, you can use OpenJDK.

  2. Pick the Right Version

    • Choose the latest version of the Java Development Kit (JDK) unless your project requires an older version.
    • Download the version that works with your computer:
      • For Windows: Download the .exe or .msi file.
      • For macOS: Download the .dmg file.
      • For Linux: Download the .tar.gz file.
  3. Download the Installer:
    Click the Download button. If you see a license agreement, just click “Accept.”

Step 2: Install Java

  1. Run the Installer: Find the file you just downloaded and double-click it.

  2. Follow the Steps: When asked where to install Java, stick with the default folder (usually something like C:\Program Files\Java\jdk-XX.X.X on Windows).

  3. Finish the Installation:

    • Keep clicking Next until the installation is done.
    • Write down the folder where Java is installed—you’ll need it later!

Step 3: Tell Your Computer Where Java Is

This step helps your computer find and use Java easily. Don’t worry if it sounds technical; just follow the steps below.

For Windows:

  1. Set Up JAVA_HOME:

    • Right-click on This PC or My Computer and click Properties.
    • Go to Advanced system settings and click Environment Variables.
    • In the System Variables section, click New, and fill out:
      • Variable Name: JAVA_HOME
      • Variable Value: The folder where Java is installed (e.g., C:\Program Files\Java\jdk-XX.X.X).
  2. Add Java to the PATH:

    • Still in Environment Variables, find the Path variable in the list under System Variables. Click Edit.
    • Click New, and paste the path to the bin folder inside your Java installation (e.g., C:\Program Files\Java\jdk-XX.X.X\bin).

For macOS/Linux:

  1. Open Terminal:
    You’ll need to edit a settings file:

    • For macOS: Use nano ~/.zshrc (default shell on newer macOS).
    • For Linux: Use nano ~/.bashrc or nano ~/.profile.
  2. Add These Lines:
    Paste the following lines at the bottom of the file:

    JAVA_HOME=/path/to/your/jdk
    PATH=$JAVA_HOME/bin:$PATH

    Replace /path/to/your/jdk with the folder where Java is installed.

  3. Save and Apply Changes:

    • Press CTRL+O, then Enter to save.
    • Press CTRL+X to exit.
    • Apply the changes with this command:
      source ~/.zshrc  # or ~/.bashrc depending on your file

Step 4: Test Your Java Installation

Let’s check if Java is set up properly!

  1. Open Command Prompt (Windows) or Terminal (macOS/Linux).

  2. Type These Commands:

    java -version

    You should see something like this:

    java version "17.0.1" 2021-10-19 LTS
    Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
    Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

    Then type:

    javac -version

    You should see:

    javac 17.0.1
  3. If You See an Error:

    • Double-check that you added the Java folder to the PATH.
    • Make sure the JAVA_HOME path points to the correct folder.

What Do These Commands Mean?

  • java -version: Checks if the Java Runtime (JRE) is installed. The JRE lets you run Java programs. - javac -version: Checks if the Java Compiler (part of the JDK) is installed. You’ll need this to write and build Java programs.

Troubleshooting Tips

If something goes wrong, here are a few things to check:

  1. "Command not found" error:

    • Make sure the bin folder from your Java installation is added to the PATH variable.
  2. Wrong Java Version:

    • If your computer is using an old version of Java, uninstall it and install the latest version.
  3. Permission Issues (macOS/Linux):

    • Run commands with sudo if needed (e.g., sudo nano ~/.zshrc).

Next Steps

Now that Java is installed, you can start coding! To make things easier, consider downloading an IDE like:

  • IntelliJ IDEA: Great for beginners with lots of helpful features.
  • Eclipse: Another popular option.
  • VS Code: Use it with the Java extensions for lightweight coding.