Menu

How to Run Java: Compile with javac and Run with java

The two-step compile-then-run cycle behind every Java program: turn .java source into bytecode with javac, then run the .class file with java.

This page includes runnable editors - edit, run, and see output instantly.

Compile, Then Run

Now that you have a JDK installed, running Java is a two-step cycle. Unlike a scripting language where you point an interpreter at a file and go, Java first compiles your human-readable source into bytecode, and then a separate step runs that bytecode on the JVM.

Those two steps map to two command-line tools that ship with the JDK:

  • javac - the compiler. It reads Main.java and writes Main.class (bytecode).
  • java - the launcher. It starts the JVM and executes the bytecode in Main.class.

You can run any self-contained example right here on the page - the editor below does both steps for you. But it's worth understanding what happens on your own machine, because that's where real projects live.

The Two Commands on Your Machine

Say you have this code saved as Main.java:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from the terminal");
    }
}

Open a terminal in the folder that holds the file and run the compiler:

javac Main.java

If the code compiles cleanly, javac prints nothing and creates a new file, Main.class, next to your source. That .class file is the bytecode - it is not readable text, and it is not tied to your operating system.

Now launch it with the java command:

java Main
Hello from the terminal

The single most common beginner mistake is here: you run java, not javac, to launch the program, and you pass the class name with no extension. It is java Main - never java Main.class and never java Main.java.

The File Name Must Match the Public Class

Java enforces a rule that surprises newcomers: a public class must live in a file with the exact same name, plus .java. A public class Main must be in Main.java. Get the capitalization wrong and javac refuses to compile.

// This is in a file called Greeting.java
public class Greeting {   // ERROR: class Greeting should be in Greeting.java... wait, it is
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

The mismatch that actually bites people is subtler - naming the file greeting.java (lowercase) while the class is Greeting, or saving Main.java but writing public class Hello. The compiler reports something like:

Main.java:1: error: class Hello is public, should be declared in a file named Hello.java

The fix is always the same: make the file name and the public class name identical, character for character.

The Shortcut: Run a Single File Directly

Since JDK 11, you can skip the explicit compile step for a single source file. The java command will compile it in memory and run it in one go - no .class file is written to disk:

java Main.java

This is perfect for quick experiments and small scripts. Notice the difference in what you pass:

  • java Main - run an already-compiled Main.class.
  • java Main.java - compile and run the source file in one shot.

The single-file mode only works when your whole program fits in one file. The moment you split code across several classes in several files, you go back to compiling with javac first. For learning the basics, single-file mode keeps the loop short.

Passing Arguments to Your Program

Remember the String[] args in main? Those are command-line arguments. Anything you type after the class name on the java command lands in that array:

On your own machine you would feed it arguments like this:

java Main hello world
First argument: hello
Total arguments: 2

The editor above can't take terminal arguments, so it runs the "no arguments" branch - but the same code handles both cases. This is how Java programs read input passed at launch time, long before you reach files or user input.

Reading the Compiler's Error Messages

When javac rejects your code, it tells you the file, the line, and what went wrong. Learning to read these messages is half of getting unstuck. Here's a classic - a missing semicolon:

public class Main {
    public static void main(String[] args) {
        System.out.println("Oops")   // no semicolon
    }
}
Main.java:3: error: ';' expected
        System.out.println("Oops")
                                  ^
1 error

The caret (^) points at where the compiler expected something. Main.java:3 is the file and line number. Resist the urge to guess - read the line, fix the one thing it names, and recompile. Compiler errors mean nothing ran yet; runtime errors (which look different) mean your program started and then failed.

A Sanity-Check Program

Run this in the editor, or save it as Main.java and do javac Main.java then java Main on your own machine. If you see all three lines, your toolchain works end to end:

Three things appear here that you'll meet properly soon: an int variable, string concatenation with +, and an array with its .length. For now it's enough that the program compiles and prints.

Next: Java Syntax

You've run a few programs, but we've been hand-waving over the punctuation - the braces, the semicolons, public static void main, and why every line looks the way it does. The next page breaks down Java's syntax piece by piece so the structure stops feeling like boilerplate and starts making sense.

Frequently Asked Questions

How do I run a Java program?

Save your code in a file named after the public class (for example Main.java), open a terminal in that folder, and run two commands: javac Main.java to compile it into bytecode, then java Main to run it. With JDK 11 or newer you can skip the first step for a single file and just run java Main.java directly.

What is the difference between javac and java?

javac is the compiler: it reads your .java source and produces a .class file full of bytecode. java is the launcher: it starts the JVM and runs the bytecode in a .class file. You compile once with javac, then run as many times as you like with java.

Why do I get "could not find or load main class"?

Almost always because you passed the wrong name to java. Use the class name, not the file name: it's java Main, not java Main.class or java Main.java. Also make sure you're in the folder that contains Main.class and that the class name matches the file name exactly, including capitalization.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED