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:
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.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.Download and Install
Once downloaded, run the installer and follow the default setup steps.Set Up Environment Variables (for Windows Users Only)
After installation, Windows users will need to configure theJAVA_HOMEenvironment variable:- Right-click on “This PC” > Select “Properties” > “Advanced system settings” > “Environment Variables.”
- Click “New” and enter the variable name
JAVA_HOMEwith the value set to the JDK installation path (for example,C:\Program Files\Java\jdk-11). - Find the
Pathvariable in “System Variables,” click “Edit,” and add%JAVA_HOME%\binto 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 | java version "11.0.x" |
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.
- Create a Java File
Open a text editor and create a new file namedHelloWorld.java. Add the following content to the file:
1 | public class HelloWorld { |
This code will display “Hello, World!” in the console.
- 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!