1.3 Setup Environment
To get started with Java, you can easily run your code in various online tools, such as:
- Programiz Java Tool
- JDoodle Java Tool
- Replit Java Tool
- OnlineGDB Java Tool
- Coding Ground Java Tool
- Ideone These platforms provide convenient ways to test and share your Java code!
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
- Installing Java Development Kit (JDK)
- Setting Up Environment Variables
- Understanding Java Development Tools
- Writing Your First Java Program
- Compiling and Running Java Code
- Conclusion
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)
Visit the official Oracle JDK download page or the OpenJDK page for an open-source alternative.
Download the latest version of the JDK compatible with your operating system.
Follow the installation instructions for your operating system:
- Windows: Run the
.exeinstaller and follow the prompts. - macOS: Download the
.dmgfile, 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.
- Windows: Run the
After installation, verify the JDK installation by running the following command in your terminal:
java -version
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:
- Open Control Panel > System and Security > System > Advanced system settings.
- Click Environment Variables.
- 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).
- Variable name:
- Edit the Path variable and add
%JAVA_HOME%\binto it.
macOS/Linux:
- Open your terminal and edit your shell configuration file (e.g., .bashrc or .zshrc).
- Add the following lines:
export JAVA_HOME=/path/to/your/jdk
export PATH=$JAVA_HOME/bin:$PATH
- Save the file and run
source ~/.bashrcorsource ~/.zshrcto 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!