01 / 21
>_

Command-Line
Compilation

Learn to compile and run Java programs from the terminal using javac and java: the foundational workflow every Java developer must know.

Java

What Is Compilation?

Computers don't understand Java directly. A compiler translates your human-readable source code into a lower-level format the machine can execute. In Java, the compiler (javac) produces bytecode: a platform-independent instruction set stored in .class files.

The Two-Step Workflow

Hello.java Source Code javac Compiler Hello.class Bytecode Step 0: Write Step 1: Compile Step 2: Run
javac
Compiles .java source into .class bytecode files
java
Launches the JVM and executes the bytecode

Why the Command Line?

IDEs like IntelliJ and Eclipse hide compilation behind buttons. But understanding the command-line workflow gives you deeper knowledge of what happens under the hood, and it's essential for server environments, build scripts, and the AP exam.

Transparency
See every step: you control what gets compiled and how
Portability
Works on any system with a JDK: no IDE required
Foundation
Build tools (Maven, Gradle) call javac and java internally

Anatomy of javac

$ javac [options] FileName.java Command Optional flags Source file(s)

Anatomy of java

$ java [options] ClassName Command Optional flags Class name (no .class)

javac vs java

javac java
Purpose Compiles source code Runs bytecode
Input FileName.java ClassName
Output .class file(s) Program execution
Extension? Yes: .java No extension
Errors Compile-time errors Runtime exceptions

Step-by-Step Workflow

1
Write your program in a text editor and save it as ClassName.java: the file name must match the public class name exactly (case-sensitive).
2
Open a terminal and navigate to the directory containing your .java file using cd.
3
Compile with javac ClassName.java. Fix any errors until compilation succeeds silently.
4
Run with java ClassName (no extension). Your program's output appears in the terminal.

Example 1: Hello World

Hello.java
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Terminal
$ javac Hello.java $ java Hello Hello, World!

Example 2: Multiple Files

When your project has multiple .java files, you can compile them all at once. Each file produces its own .class file, but you run only the class containing main.

Terminal
$ javac Main.java Helper.java # Creates Main.class and Helper.class $ java Main Result: 42

Example 3: Packages

When your class declares a package, the directory structure must match the package name. You compile from the project root and specify the full path.

com/myapp/App.java
package com.myapp; public class App { public static void main(String[] args) { System.out.println("Running from a package!"); } }
Terminal (from project root)
$ javac com/myapp/App.java $ java com.myapp.App Running from a package!

Understanding the Classpath

The classpath tells the JVM where to find .class files. By default it searches the current directory. You can set it explicitly with the -cp flag.

Using -cp (classpath)
# Compile into a 'build' directory $ javac -d build Hello.java # Run from 'build' directory $ java -cp build Hello Hello, World!
-d flag
Tells javac where to place the output .class files
-cp flag
Tells java where to search for .class files at runtime

What Happens Inside?

Hello.java javac: Lexer → Parser → Bytecode Hello.class (Bytecode) JVM: ClassLoader → Interpreter/JIT Compile-time Runtime

The main Method

The JVM needs an entry point to begin execution. It looks for a method with this exact signature in the class you specify:

public static void main(String[] args)
public
Accessible to the JVM from outside your class
static
Called without creating an object instance
void
Returns nothing to the operating system
String[] args
Receives command-line arguments as an array

Common Mistakes

✕ Wrong extension
Running java Hello.class instead of java Hello. The java command takes a class name, not a file name.
✕ Case mismatch
File named hello.java but class declared as Hello. Java is case-sensitive: they must match exactly.
✕ Forgetting to recompile
Editing the .java file but running the old .class file. You must run javac again after every change.

Quick Reference Table

Task Command Example
Compile one file javac File.java javac Hello.java
Compile all files javac *.java javac *.java
Run a class java ClassName java Hello
Run with package java pkg.Class java com.myapp.App
Set output dir javac -d dir File.java javac -d build App.java
Set classpath java -cp dir Class java -cp build App

Question 1 of 5

Which command compiles a Java source file named Calculator.java?

Question 2 of 5

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

Question 3 of 5

Your file is named myApp.java and contains public class MyApp. What happens when you run javac myApp.java?

Question 4 of 5

What does the -cp (or -classpath) flag do when used with the java command?

Question 5 of 5

Your class is in package com.school and the file is at com/school/App.java. Which pair of commands compiles and runs it correctly from the project root?