Tutorials

Java For Beginners

Key Takeaways

  • Install a Java Development Kit (JDK), then verify both the compiler with javac -version and the runtime with java -version.
  • For this example, save public class HelloWorld in HelloWorld.java. Java is case-sensitive.
  • Compile with javac HelloWorld.java, then launch the compiled class with java HelloWorld.
  • Java 11 and later also support launching a source file directly with java HelloWorld.java.

To write and run your first Java program, install a JDK, create a plain-text file named HelloWorld.java, compile it with javac, and launch it with java. This Java beginner tutorial walks through that workflow from the command line.

You don't need an IDE—an integrated development environment—such as IntelliJ IDEA or Eclipse. A plain-text editor and a terminal are enough for this exercise.

Getting Started With Java: Install the JDK

Choose a JDK distribution and version

This tutorial assumes JDK 25, a long-term support (LTS) release introduced in September 2025. The same HelloWorld example also works with JDK 21, another LTS release. LTS identifies releases selected for longer-term maintenance; support periods and licensing depend on your vendor.

A JDK includes javac, the Java compiler, and the runtime components needed to run your program. A runtime-only installation can run compiled applications but doesn't provide the compiler needed for this tutorial.

For a concrete starting point, download a JDK 25 package from Eclipse Temurin's releases page. Select your operating system and processor architecture, and choose JDK, not JRE. If you're choosing Java for an existing project rather than this exercise, read which Java version you should use.

Install it for your operating system

Temurin provides these platform-specific installation methods. If you download an archive instead, follow its archive installation instructions, including adding the extracted JDK's bin directory to PATH. PATH is the list of directories your shell searches for commands.

Verify the compiler and runtime

Open a new terminal after installation: PowerShell or Command Prompt on Windows, or a terminal on macOS or Linux. Run both commands:

javac -version
java -version

javac -version reports the compiler version; java -version reports the runtime version. Exact output varies by distribution and update, so don't expect a particular vendor name or release date. For this setup, both should identify major version 25.

For future updates, use your chosen vendor's installer or package manager to install a current update for your selected major version, then repeat both checks. Don't assume that installing another JDK automatically changes the commands your terminal uses.

Java Hello World: Create the Source File

Create a working directory and enter it:

mkdir java-beginner
cd java-beginner

In your text editor, create a file inside that directory named HelloWorld.java. Save the following code as plain text, not as a word-processing document:

HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Here's what you're looking at:

  • class HelloWorld declares a class—a named container for this program's code.
  • main is the entry point used to start this program. Here, public makes the method accessible, static lets it run without first creating a HelloWorld object, and void means it returns no value.
  • String[] args receives command-line arguments as an array of strings. This program doesn't use them.
  • System.out.println("Hello World") writes the message to standard output, normally your terminal, and ends the line.

The braces group the class and method bodies. Keep the semicolon after the print statement. The traditional main declaration shown here remains a valid way to launch this program.

Match the public class name and filename

When compiling with javac, a public top-level class—a class declared outside another class—must use its class name as the source filename. That means public class HelloWorld belongs in HelloWorld.java. This isn't a rule that every Java class needs its own matching file.

Java is case-sensitive: HelloWorld and helloWorld are different names. Copy System, String, and println with the capitalization shown.

Compile and Run Your Java Program

Compile the source with javac

Save your file and return to the terminal inside java-beginner. Compile it:

javac HelloWorld.java

For this example, successful compilation normally produces no terminal output and creates HelloWorld.class beside the source file. That class file contains Java bytecode, the compiled instructions executed by the Java Virtual Machine (JVM). If compilation reports errors, fix them before proceeding.

Launch the class with java

Run the compiled program:

java HelloWorld

The expected output is:

Hello World

Use the class name, not the class filename: java HelloWorld, not java HelloWorld.class. The launcher starts the JVM, loads the class, and invokes its entry point.

Now change the message, save the file, compile again, and run again. In this workflow, java HelloWorld runs the compiled class; it doesn't automatically recompile your edited source.

Optional: Launch the source file directly

Starting with Java 11, you can skip the separate compiler command for a single-file program. With a source-only copy of this example, run:

java HelloWorld.java

This is source-file launch mode. The launcher compiles the source in memory and runs it without writing a HelloWorld.class file to disk. It's convenient for experiments, but the explicit compile-and-run workflow is worth learning because it shows the difference between source code and compiled code.

Common Mistakes and How to Fix Them

javac says command not found or is not recognized

If java works but javac doesn't, you may have a runtime-only installation or a JDK whose compiler isn't on PATH. Confirm that you installed a JDK, then add its actual bin directory to PATH using your vendor's setup instructions. Setting JAVA_HOME alone isn't enough to make the shell find javac.

Open a new terminal and repeat both version checks. If they identify different major versions, correct your executable search paths so the compiler and launcher use the intended JDK.

The compiler cannot find HelloWorld.java

Check the files in your current directory with dir on Windows or ls on macOS/Linux. Return to the folder containing your source file, and make sure the editor didn't save it as HelloWorld.java.txt.

The compiler reports a syntax error

Start with the first error and its line number. Compare your code with the example, checking capitalization, straight quotation marks, the semicolon, and matching braces; fix it, save, and compile again. One missing character can cause several later diagnostics.

Java could not find or load the main class

Confirm that compilation succeeded and that HelloWorld.class exists. Run from its directory, or explicitly provide the class path—the location Java searches for compiled classes. For example, from the parent of java-beginner:

java -cp java-beginner HelloWorld

Here, -cp tells the launcher to search java-beginner for classes. If the problem persists, follow our guide to the Java could not find or load main class error.

What to Try After Hello World

Before adding more tools, make two small changes: print a different message, then add a second print statement. Recompile after each change so the edit–compile–run loop becomes familiar.

Next, learn Java primitive data types, practice decisions and loops with Java control flow statements, and explore Java arrays. After that, move on to organizing programs with objects and packages.

FAQ

Do I need a JDK or a JRE to write Java programs?

Install a JDK: it includes the compiler and runtime components needed to develop and run Java programs. A runtime-only JRE doesn't include javac, and standalone runtime availability depends on the vendor.

How do I compile and run a Java file from the command line?

For this example, open a terminal in the directory containing HelloWorld.java and run javac HelloWorld.java. After compilation succeeds, run java HelloWorld without a .class extension.

Can I run a Java source file without compiling it first?

With JDK 11 or later, use java HelloWorld.java to launch this single-file program without a separate javac command. Compilation still happens, but the launcher performs it in memory before running the program.

Why does javac say command not found?

Your shell cannot locate the Java compiler. Install a JDK rather than only a runtime, ensure the JDK's bin directory is on PATH, and open a new terminal before retrying javac -version.

Why must a public Java class have the same name as its file?

In normal javac compilation, a public top-level class must be stored in a source file with the same name and a .java extension, which helps the compiler locate named types. For public class HelloWorld, use HelloWorld.java, including the exact capitalization.

Further Reading

Written by Sam Erickson

Software engineer. I write every article on StackChief myself. Author of Mastering the Tech Interview. More about me