"How to Set Up Your Java Development Environment and Run Your First 'Hello World' Program"
Meta Description:
"Learn how to set up your Java development environment by installing the JDK, JRE, and IDEs like IntelliJ IDEA, Eclipse, or NetBeans. Start your Java journey with a step-by-step guide to writing and running your first 'Hello World' program."
Introduction:
Java is a powerful, platform-independent programming language that has been at the forefront of software development for decades. Before diving into Java programming, it's essential to set up your development environment properly. In this tutorial, we’ll walk you through installing the Java Development Kit (JDK), setting up an IDE like IntelliJ IDEA, Eclipse, or NetBeans, and writing your first "Hello World" program.
By the end of this post, you'll have a fully functioning Java development setup, ready to create and run Java applications.
1. Installing Java Development Kit (JDK) & Java Runtime Environment (JRE)
Before writing any Java code, you'll need to install the JDK and JRE. The JDK contains everything required to write, compile, and run Java applications, while the JRE is necessary for running Java applications.
Step 1.1: Download the JDK
- Visit the official Oracle JDK download page.
- Select the latest version of Java SE and choose the installer suitable for your operating system (Windows, macOS, Linux).
- Download the installer and run it.
Step 1.2: Install the JDK
- Once the installer is downloaded, open it and follow the installation wizard.
- Set the installation directory (default is fine).
- After installation, verify if Java is installed correctly by opening your Command Prompt (Windows) or Terminal (macOS/Linux) and typing:
You should see the installed version of Java.
Step 1.3: Setting Up the Environment Variable (Optional for Some OS)
To compile and run Java programs from any directory, you need to add the JDK bin folder to your system’s PATH.
On Windows:
- Go to System Properties > Advanced > Environment Variables.
- Under "System Variables," find Path and click Edit.
- Add the JDK bin directory (e.g.,
C:\Program Files\Java\jdk-17\bin
) to the list and save.
On macOS/Linux:
- Open Terminal and edit your
.bash_profile
or.zshrc
file. - Add the following line:
- Save and reload your terminal:
- Open Terminal and edit your
2. Installing an Integrated Development Environment (IDE)
An IDE provides a comprehensive environment for writing, running, and debugging your Java code. Let's look at setting up some popular IDEs for Java development.
Step 2.1: Installing IntelliJ IDEA
- Visit the IntelliJ IDEA website and download the Community Edition (free version).
- Follow the installation steps for your OS (Windows/macOS/Linux).
- After installation, launch IntelliJ and configure the JDK path when prompted.
Step 2.2: Installing Eclipse
- Go to the Eclipse IDE download page.
- Download the installer and select "Eclipse IDE for Java Developers."
- Follow the installation steps, launch Eclipse, and configure the JDK.
Step 2.3: Installing NetBeans
- Head to the Apache NetBeans website.
- Download the version for Java SE Development.
- Follow the instructions to install and launch NetBeans.
3. Writing and Running Your First Java Program
Now that the JDK and an IDE are installed, it's time to write your first Java program, the classic "Hello World" example.
Step 3.1: Create a New Java Project
In your chosen IDE (IntelliJ IDEA, Eclipse, or NetBeans):
Create a New Project:
- In IntelliJ: File > New > Project and select Java.
- In Eclipse: File > New > Java Project.
- In NetBeans: File > New Project > Java > Java Application.
Name your project (e.g., "HelloWorld") and click Finish.
Step 3.2: Write the Code
In the project structure, locate the src
folder and create a new Java Class called HelloWorld
. Write the following code:
Step 3.3: Compile and Run the Program
Once your code is ready, run the program:
- In IntelliJ IDEA, right-click on the class and select Run.
- In Eclipse, click the green Run button.
- In NetBeans, click the green Play icon.
The console should display:
Congratulations! You've successfully written and executed your first Java program.
4. Compilation and Execution (Manual via Command Line)
If you prefer to compile and run your Java program using the command line, here’s how to do it:
Step 4.1: Writing the Code in a Text Editor
- Open any text editor (like Notepad++ or VS Code) and write the following code:
- Save the file as
HelloWorld.java
.
Step 4.2: Compiling the Program
- Open your Command Prompt (Windows) or Terminal (macOS/Linux).
- Navigate to the directory where you saved
HelloWorld.java
using thecd
command. - Compile the Java file using:
This will generate a file called
HelloWorld.class
.
Step 4.3: Running the Program
To run the compiled Java program, use the following command:
The output will be:
5. Exercise: Install Java, Set Up the IDE, and Run "Hello World"
Now it’s your turn! Follow these steps to:
- Install the JDK and set up your system's environment variables.
- Install your preferred IDE (IntelliJ IDEA, Eclipse, or NetBeans).
- Write and run your first Java "Hello World" program either in the IDE or using the command line.