Course contentsShow
← Java
Lesson 13 of 2,8701. Java Setup and First ProgramFree lesson

Running with java

Executing compiled Java programs using the java command and understanding the class name argument.

Running with java

What you'll learn: How to execute your compiled Java program using the java command and why the class name matters.

From Bytecode to Execution

You've already compiled your .java file into bytecode (a .class file) using javac. Now it's time to run it! The java command launches the Java Virtual Machine (JVM), which reads your bytecode and executes your program.

The java Command Syntax

The basic syntax is straightforward:

java ClassName

Important: You use the class name (without the .class extension), not the filename. If your file is HelloWorld.java and contains public class HelloWorld, you run:

java HelloWorld

Not java HelloWorld.class — that's a common beginner mistake!

How It Works

Think of the java command like pressing "play" on a music file. You've already converted your sheet music (source code) into a digital format (bytecode). Now the JVM acts as your media player, reading that bytecode and performing the instructions.

When you type java HelloWorld, the JVM:

  1. Locates the HelloWorld.class file
  2. Loads it into memory
  3. Finds the main method (the entry point you learned about earlier)
  4. Starts executing the code line by line

Seeing It in Action

After running java HelloWorld, you should see your program's output in the terminal. If you used the Hello World program from earlier, you'll see your printed message appear immediately.

If you get an error like "Could not find or load main class," double-check that:

  • You're in the correct directory (where the .class file is)
  • You spelled the class name exactly right (Java is case-sensitive!)
  • You compiled successfully first with javac

Key Takeaway: The java command runs your compiled bytecode by launching the JVM and executing the main method of the class you specify—always use the class name, never the .class extension.