How to Install the Java Environment and Run Your First Code

Introduction

Java is a powerful and widely-used programming language behind many large-scale applications and systems. For beginners, learning Java is an excellent way to grasp programming fundamentals and build a strong foundation for future growth. Before diving into writing Java code, the first step is to set up your Java development environment. Let’s walk through this setup step-by-step and get to the fun part—running your first piece of Java code!

Step 1: Installing the Java JDK

The JDK (Java Development Kit) is an essential toolkit for writing and running Java code. Here’s how to get it installed:

  1. Visit the Oracle Website to Download the JDK
    Open the Oracle website (or simply search for “Java JDK download”). You’ll see several versions available—choose a stable one, such as JDK 11 or JDK 17.

  2. Select the Version for Your Operating System
    Windows users should download the Windows version, Mac users should select the macOS version, and Linux users should download the Linux version.

  3. Download and Install
    Once downloaded, run the installer and follow the default setup steps.

  4. Set Up Environment Variables (for Windows Users Only)
    After installation, Windows users will need to configure the JAVA_HOME environment variable:

    • Right-click on “This PC” > Select “Properties” > “Advanced system settings” > “Environment Variables.”
    • Click “New” and enter the variable name JAVA_HOME with the value set to the JDK installation path (for example, C:\Program Files\Java\jdk-11).
    • Find the Path variable in “System Variables,” click “Edit,” and add %JAVA_HOME%\bin to the path.

Step 2: Verify the Java Installation

After following the above steps, open the Command Prompt (Windows users can search for cmd in the Start menu, while Mac users can use the Terminal) and type the following command:

1
java -version

If the installation was successful, you’ll see something like this:

1
2
3
java version "11.0.x" 
Java(TM) SE Runtime Environment ...
Java HotSpot(TM) 64-Bit Server VM ...

This output means that Java is ready to go!

Step 3: Write and Run Your First Java Code

Now, let’s write and execute your first Java code to experience the thrill of programming.

  1. Create a Java File
    Open a text editor and create a new file named HelloWorld.java. Add the following content to the file:
1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

This code will display “Hello, World!” in the console.

  1. Compile and Run the Code
    In the Command Prompt or Terminal, navigate to the directory where the code file is saved, then compile the code by entering:
1
javac HelloWorld.java

If there are no errors, a file named HelloWorld.class will be created. Now, run the program with:

1
java HelloWorld

You should see the output:

1
Hello, World!

Congratulations! You’ve successfully written and run your first Java program. By completing this, you’ve gone from environment setup to program execution. From here, you’re ready to dive into Java basics and continue your programming journey!