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
javac Hello.java # 1. compile: Hello.java → Hello.class (bytecode)
java Hello # 2. run: start a JVM and run the class Hellojavactakes a file name (with.java).javatakes a class name (without.class).
02Two classes in one program
Real programs have many classes. Here App uses a second class, Greeter:
public class Greeter {
public String greet(String name) {
return "Hello, " + name + "!";
}
}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:
javac Greeter.java App.java
java AppHello, Asha!
03Packages and folders
Bigger projects group classes into packages, which are just folders. A class in package com.example.shop starts with:
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:
javac com/example/shop/Cart.java
java com.example.shop.Cart04The 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:
java -cp out com.example.shop.CartThis 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
| Message | What it means | Fix |
|---|---|---|
error: class Hello is public, should be declared in a file named Hello.java | File name and class name differ | Rename the file to match the public class |
error: ';' expected | A statement is missing its semicolon | Add ; at the line shown |
Error: Could not find or load main class Hello | The JVM can't find the class on the classpath | Run from the right folder, use the full package name, or set -cp |
Error: Main method not found in class Hello | No 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
.classfiles (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. Which command compiles Hello.java?
2. How do you run class Cart in package com.example.shop?
3. What is the classpath?
4. You see "Could not find or load main class Hello". Which is NOT a likely cause?
5. How many public classes can one .java file contain?