Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Installing Java

Installing Java is a crucial step for developers looking to create and run Java applications. Java provides a robust platform for building cross-platform applications, and setting it up correctly is essential for development and execution.

Prerequisites

Before installing Java, ensure you have:

  • Administrative privileges on your machine.
  • An internet connection to download the Java Development Kit (JDK).

Steps to Install Java

Step 1: Download the JDK

  1. Visit the Official Website: Go to the Oracle JDK download page or OpenJDK for open-source versions.
  2. Select the JDK Version: Choose the version of the JDK you wish to install. The latest stable version is recommended.
  3. Choose the Installer: Download the installer compatible with your operating system (Windows, macOS, or Linux).

Step 2: Install the JDK

Windows

  1. Run the Installer: Double-click the downloaded .exe file.
  2. Follow the Installation Wizard: Proceed through the installation wizard, accepting the default settings or customizing as needed.
  3. Set Environment Variables:
    • Open the System Properties by right-clicking on 'This PC' and selecting 'Properties'.
    • Click on 'Advanced system settings' and then 'Environment Variables'.
    • Under 'System variables', click 'New' and add JAVA_HOME with the path to the JDK installation directory.
    • Edit the 'Path' variable and add %JAVA_HOME%\bin.

macOS

  1. Run the Installer: Open the downloaded .dmg file and run the installer package.
  2. Follow the Installation Instructions: Follow the on-screen instructions to complete the installation.
  3. Set Environment Variables:
    • Open Terminal.
    • Edit the .bash_profile or .zshrc file in your home directory to include:
      export JAVA_HOME=$(/usr/libexec/java_home)
      export PATH=$JAVA_HOME/bin:$PATH
    • Save the file and run source ~/.bash_profile or source ~/.zshrc.

Linux

  1. Use Package Manager:
    • For Debian-based systems (e.g., Ubuntu), run:
      sudo apt update
      sudo apt install openjdk-11-jdk
    • For RPM-based systems (e.g., Fedora), run:
      sudo dnf install java-11-openjdk-devel
  2. Set Environment Variables:
    • Open the terminal and edit the .bashrc file:
      export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
      export PATH=$JAVA_HOME/bin:$PATH
    • Save the file and run source ~/.bashrc.

Step 3: Verify the Installation

  1. Open Command Prompt/Terminal.
  2. Check Java Version: Run the command:
    java -version
    This should display the installed Java version, confirming a successful installation.

Tips and Best Practices

  • Keep Java Updated: Regularly check for updates to ensure you have the latest security patches and features.
  • Use the LTS Version: For stability, use the Long Term Support (LTS) version of Java for production environments.
  • Manage Multiple Versions: Use tools like SDKMAN! on macOS/Linux or jEnv to manage multiple Java versions.
  • Set JAVA_HOME: Always set the JAVA_HOME environment variable to avoid issues with Java-based applications finding the correct JDK.