Compiling and Running from the Command Line
IDEs hide the build behind a green Run button, which is convenient until something breaks. Then you need to know what that button actually does. Underneath, it's always the same two tools: javac to compile and java to run. Once you can use them by hand, build errors, CI pipelines and server deployments stop being mysterious.
The Two Commands
| Command | Input | Output |
|---|---|---|
javac Bill.java | A file name, with .java | Bill.class in the same folder |
java Bill | A class name, without .class | Runs the program |
public class Bill {
public static void main(String[] args) {
int tea = 20;
int samosa = 15;
int total = tea * 2 + samosa * 3;
System.out.println("Total: Rs. " + total);
}
}Total: Rs. 85javac Bill.java
java BillHow it works
- Lines 3 and 4 store two prices.
- Line 5 works out two teas and three samosas:
20 * 2 + 15 * 3. Multiplication happens before addition, so the result is40 + 45 = 85. - Line 6 prints it.
Try it yourself: add a third item, int coffee = 25;, and include one coffee in the total. What should it print?
Compiling More Than One File
Real programs have many classes, usually one per file. Pass all of them to javac, or use a wildcard:
javac Main.java Fare.java
javac *.javaIf Main uses Fare, javac also compiles Fare.java on its own when it can find it in the same folder. Run the program by naming the class that has main:
java MainKeep .class Files Separate with -d
Mixing .java and .class files in one folder gets messy fast. The -d option tells javac where to write the .class files:
javac -d out Bill.javaThis creates a folder named out with Bill.class inside. To run it, tell java where to look with -cp (short for classpath):
java -cp out BillThe classpath is the list of folders and .jar files where the JVM searches for classes. By default it's the current folder, which is why plain java Bill works when Bill.class is right there.
Useful Options
| Command | What it does |
|---|---|
javac --version | Prints the compiler version |
java --version | Prints the runtime version (-version also works) |
javac -d out *.java | Compiles every file in the folder into out |
java -cp out Main | Runs Main, looking for classes in out |
javac --release 17 Bill.java | Compiles for Java 17, so the class runs on a Java 17 JVM |
java -cp out Main Srihitha 3 | Passes Srihitha and 3 as command-line arguments |
Common Mistakes
Running with .class on the end. java Bill.class makes Java look for a class literally named Bill.class:
Error: Could not find or load main class Bill.class
Caused by: java.lang.ClassNotFoundException: Bill.classRunning from the wrong folder. If you compiled into out but run java Bill from the project folder, the JVM can't find the class. You get the same Could not find or load main class Bill error. Add -cp out, or cd into the right folder.
Running a class that has no main. java Fare on a helper class prints Error: Main method not found in class Fare. Run the class that contains main.
Editing the code and running the old .class. If you change Bill.java but forget to run javac again, java Bill runs the old version. Compile after every change, or use java Bill.java, which always compiles first.
Practice
- Easy: Compile
Bill.javainto a folder namedclasses, then run it from the parent folder. - Harder: Create two files:
Main.java, which printsFare.total(450, 3), andFare.java, with astatic int total(int fare, int people)method. Compile both with one command and runMain.
Key Takeaways
Was this tutorial helpful?