Skip to main content

1.3 Setup Environment

ONLINE TOOLS

To get started with Java, you can easily run your code in various online tools, such as:

Let’s dive into coding: setting up the Java development environment is your first step as a developer, and this guide will cover installation and the Java execution environment.

Table of Contents


Prerequisites

Before you begin, ensure that your system meets the following requirements:

  • A 64-bit operating system (Windows, macOS, or Linux)
  • Administrator privileges to install software
  • Basic knowledge of the command-line interface (CLI)

Installing Java Development Kit (JDK)

  1. Visit the official Oracle JDK download page or the OpenJDK page for an open-source alternative.

  2. Download the latest version of the JDK compatible with your operating system.

  3. Follow the installation instructions for your operating system:

    • Windows: Run the .exe installer and follow the prompts.
    • macOS: Download the .dmg file, open it, and drag the JDK to your Applications folder.
    • Linux: Use your distribution's package manager or download the JDK tar file and follow the manual installation steps.
  4. After installation, verify the JDK installation by running the following command in your terminal:

java -version
tip

You should see something like:

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)

Setting Up Environment Variables

To run Java from any directory in your system, you need to set the JAVA_HOME environment variable and update your system’s PATH.

Windows:

  1. Open Control Panel > System and Security > System > Advanced system settings.
  2. Click Environment Variables.
  3. Under System Variables, click New and set the following:
    • Variable name: JAVA_HOME
    • Variable value: Path to your JDK installation (e.g., C:\Program Files\Java\jdk-17.0.1).
  4. Edit the Path variable and add %JAVA_HOME%\bin to it.

macOS/Linux:

  1. Open your terminal and edit your shell configuration file (e.g., .bashrc or .zshrc).
  2. Add the following lines:
export JAVA_HOME=/path/to/your/jdk
export PATH=$JAVA_HOME/bin:$PATH
  1. Save the file and run source ~/.bashrc or source ~/.zshrc to apply the changes.

Understanding Java Development Tools

Java Compiler (javac)

The javac tool is the Java compiler that converts Java source code into bytecode. The bytecode is platform-independent and can be executed by any system that has a Java Virtual Machine (JVM).

Java Runtime Environment (JRE)

The JRE provides libraries, Java classes, and other components needed to run Java applications. It does not include the development tools (such as the compiler) found in the JDK.

Java Virtual Machine (JVM)

The JVM is the engine that runs Java applications. It interprets the compiled bytecode and converts it into machine code for the host system. The JVM also handles memory management, garbage collection, and other runtime optimizations.

1.4 Writing & Executing Your First Java Program

Create a new file named HelloWorld.java in your working directory and add the following code:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Writing Your First Java Program

This simple program prints "Hello, World!" to the console.

Compiling and Running Java Code

Compile the program:

Open your terminal or command prompt, navigate to the directory where HelloWorld.java is located, and run:

javac HelloWorld.java

This will generate a HelloWorld.class file, which contains the bytecode.

Run the program:

Execute the compiled bytecode using the Java command:

java HelloWorld

You should see the following output: Hello, World!

Conclusion

Congratulations! You have successfully set up your Java environment and run your first Java program. Understanding the Java execution environment, including the JDK, JVM, and JRE, is crucial to becoming proficient in Java development. You are now ready to explore more advanced concepts and start building real-world applications!