Skip to content
Veltoryx Technologies
FreeLesson 1.5Beginner3 min readUpdated 30 October 2026

Compile and run from the terminal

Use javac and java yourself, understand class files, packages and the classpath, and read the errors you'll see most often.

Editors like IntelliJ hide the build steps behind a green Run button. That's convenient, but interviewers often ask what happens underneath. Let's do it by hand once, so you really understand it.

01Two commands: javac and java

Terminal
javac Hello.java   # 1. compile: Hello.java → Hello.class (bytecode)
java Hello         # 2. run: start a JVM and run the class Hello
  • javac takes a file name (with .java).
  • java takes a class name (without .class).

02Two classes in one program

Real programs have many classes. Here App uses a second class, Greeter:

Greeter.java
public class Greeter {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}
App.java
public class App {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.greet("Asha"));
    }
}

Compile both, then run the class that has main:

Terminal
javac Greeter.java App.java
java App
Expected output
Hello, Asha!

03Packages and folders

Bigger projects group classes into packages, which are just folders. A class in package com.example.shop starts with:

com/example/shop/Cart.java
package com.example.shop;

public class Cart {
    public static void main(String[] args) {
        System.out.println("Cart is empty");
    }
}

The file must live in the matching folder: com/example/shop/Cart.java. You compile it with the path, and run it with the full class name, using dots:

Terminal
javac com/example/shop/Cart.java
java com.example.shop.Cart

04The classpath

When the JVM needs a class, it searches the classpath: a list of folders and .jar files. By default it's the current folder. You can set it with -cp:

Terminal
java -cp out com.example.shop.Cart

This says "look for classes inside the out folder". A JAR file is simply a zip of .class files, so you can put libraries on the classpath too: -cp out:libs/gson.jar (use ; instead of : on Windows).

05Errors you'll meet most

MessageWhat it meansFix
error: class Hello is public, should be declared in a file named Hello.javaFile name and class name differRename the file to match the public class
error: ';' expectedA statement is missing its semicolonAdd ; at the line shown
Error: Could not find or load main class HelloThe JVM can't find the class on the classpathRun from the right folder, use the full package name, or set -cp
Error: Main method not found in class HelloNo public static void main(String[] args)Add a correct main method

06In the interview

Common follow-up questions:

  • What is the classpath? The list of folders and JAR files where the JVM and compiler look for classes.
  • What is a JAR file? A zip archive of .class files (plus resources), used to package and share Java code.
  • Can a .java file contain more than one class? Yes, but only one can be public, and the file must be named after it.

07Check your understanding

Quiz · 5 questions

0 of 5 answered

  1. 1. Which command compiles Hello.java?

  2. 2. How do you run class Cart in package com.example.shop?

  3. 3. What is the classpath?

  4. 4. You see "Could not find or load main class Hello". Which is NOT a likely cause?

  5. 5. How many public classes can one .java file contain?