Write and Run Your First Java Program in 10 Minutes
If you’ve ever stared at a blank screen and wondered how to get a simple “Hello, World!” showing up, you’re not alone. The good news is that with a few clicks and a couple of commands you can have a Java program running in less time than it takes to brew a cup of coffee. Let’s walk through it together, step by step, so you can see the magic happen right now.
What You Need Before You Start
Before we dive into code, make sure you have the basics ready. You don’t need a fancy setup – just three things:
- Java Development Kit (JDK) – This is the set of tools that lets you write and run Java code.
- A text editor – Anything that lets you type plain text. Notepad, VS Code, or even a simple IDE like IntelliJ IDEA will do.
- A command line window – On Windows that’s Command Prompt or PowerShell, on macOS or Linux it’s the Terminal.
If any of these sound unfamiliar, don’t worry. I’ll show you how to get each one in a minute.
Install the JDK
- Go to the official Oracle JDK page or the OpenJDK site. I usually grab the OpenJDK because it’s free and works everywhere.
- Download the installer that matches your operating system.
- Run the installer and follow the prompts. The default options are fine for most people.
- After installation, open a command line window and type
java -version. You should see something likeopenjdk version "17.0.2"– that means Java is ready.
If the command says “not recognized,” you may need to add the JDK’s bin folder to your system’s PATH variable. A quick Google search for “add Java to PATH” will give you step‑by‑step instructions for your OS.
Choose an Editor
When I first learned Java, I used Notepad and spent a lot of time fixing tiny typos. These days I love Visual Studio Code because it’s lightweight, has good Java extensions, and gives you instant feedback. Install VS Code, then add the “Extension Pack for Java” from the marketplace. If you prefer a full‑featured IDE, IntelliJ Community Edition is also a solid choice.
Write the Code
Now that the tools are set, let’s write the simplest Java program ever.
Create a File
- Open your editor and create a new file.
- Save it as
HelloWorld.java. The file name must match the class name we’ll use, and the extension.javatells the compiler it’s a Java source file.
The Classic HelloWorld
Copy the following code into the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let’s break it down in plain language:
public class HelloWorld– This declares a class namedHelloWorld. In Java, every program lives inside a class.public static void main(String[] args)– This line defines the main method, the entry point where the program starts. Think of it as the front door.System.out.println("Hello, World!");– This tells Java to print the text inside the quotes to the screen, followed by a new line.
That’s it! Save the file and we’re ready to run it.
Compile and Run
Java code is written in a human‑readable form, but the computer needs it in bytecode. The javac command does that conversion.
Using the Command Line
- Open your command line window.
- Navigate to the folder where you saved
HelloWorld.java. On Windows you can typecd path\to\folder, on macOS/Linuxcd /path/to/folder. - Compile the program with:
javac HelloWorld.java
If everything is correct, you’ll see no output and a new file HelloWorld.class will appear. That file contains the bytecode.
- Run the program with:
java HelloWorld
You should see:
Hello, World!
Congratulations – you just wrote, compiled, and ran your first Java program!
Using an IDE
If you prefer a graphical approach, open the folder in VS Code or IntelliJ.
- In VS Code, the Java extension will automatically detect the
mainmethod. Click the Run button that appears above the method, or pressF5. - In IntelliJ, right‑click the file and choose Run 'HelloWorld.main()'.
The IDE handles the compile step for you, so you only see the output window with “Hello, World!”.
Common Pitfalls and How to Fix Them
Even with a simple program, beginners hit a few snags. Here are the most common and quick fixes.
“class HelloWorld is public, should be declared in a file named HelloWorld.java”
Java is strict about file names. Make sure the file name matches the public class name exactly, including case. Rename the file if needed.
“cannot find symbol: variable System”
This usually means the JDK isn’t set up correctly. Double‑check that you installed the JDK (not just the JRE) and that the java and javac commands point to the same version.
“java: command not found”
Your PATH variable probably doesn’t include the JDK’s bin folder. Add it, then restart the command line window.
“Exception in thread "main" java.lang.NoClassDefFoundError”
You might be trying to run the program from the wrong folder. Make sure you’re in the same directory that contains the compiled .class file, or give the full package name if you used one.
A Quick Recap
- Install the JDK and verify with
java -version. - Choose a simple editor like VS Code.
- Create
HelloWorld.javawith the classic code. - Compile with
javac HelloWorld.java. - Run with
java HelloWorld.
If you follow these steps, you’ll have a working Java program in under ten minutes. From here you can start experimenting – change the message, add more println lines, or try reading input from the user. The sky’s the limit, and the best way to learn is by doing.
Happy coding, and welcome to the Java Jumpstart community!
- → Beginner's Checklist: Essential Drone Settings for Sharp Aerial Photos @skylenssnapshots
- → Bitcoin Wallets for Beginners: Simple Steps to Secure Your First Coins @bitcoinbasics
- → Step‑by‑Step Guide to Buying Your First Bitcoin Safely @bitcoinbasics
- → How to Build Your First Investment Portfolio with Just $100: A Step‑by‑Step Guide @investing101
- → Step-by-Step CrossFit Starter Plan: 4 Weeks to Build Strength Safely @crossfitstarter