Lesson 5 of 618 minModule progress 0%

Module 1: Start Coding in Java

Run a Java File from Terminal and from an IDE

Compile and run a simple Java file locally, then see how an IDE does the same job with less manual typing.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-16

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, edited by hand, and checked against current Java 25 documentation and runnable examples.

Learning goals

  • Compile a `.java` file with `javac`
  • Run a compiled class with `java`
  • Understand what your IDE automates for you

Before you start

  • JDK 25 is installed and `java` / `javac` work from your terminal

Terminal flow: Save a file such as Main.java, compile it with javac Main.java, then run it with java Main. The java command uses the class name, not the file name with .java or .class.

IDE flow: IntelliJ IDEA or VS Code can compile and run the same program through a Run button. The IDE is convenient, but it still relies on the same underlying Java tools.

Why this matters: Knowing both paths makes you less dependent on one editor and helps you understand error messages when something breaks.

Keep it simple: At this stage, one class, one file, one main method is enough.

Runnable examples

A local file you can compile and run

public class Main {
    public static void main(String[] args) {
        System.out.println("Running from my own machine.");
    }
}

Expected output

Running from my own machine.

The terminal commands

javac Main.java
java Main

Expected output

Compiles the source file, then runs the compiled class.

Common mistakes

Running `java Main.java` after compiling

After compilation, run `java Main` with the class name only.

Using a file name that does not match the public class name

If the class is `public class Main`, the file should be `Main.java`.

Mini exercise

Create a local file that prints your city, compile it, and run it from the terminal.

Summary

  • `javac` compiles source code into class files.
  • `java` runs the compiled class by name.
  • An IDE automates the same steps for you.

Next step

Finish the module with a short troubleshooting lesson so the first common errors feel normal instead of scary.

Sources used

Advertisement

Lesson check

After compiling `Main.java`, which command runs the program?

Next lesson →