Command Line Compilation & Execution
Command line compilation and execution explain how Java programs are compiled and run without any IDE. This is important for understanding Java internals, CI/CD pipelines, servers, and interview questions.
Prerequisites
- Java JDK installed
- JAVA_HOME configured
- javac and java available in PATH
Verify installation:
java -version javac -version
Step 1: Create a Java Source File
Create a file named Hello.java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Important Rule: The file name must match the public class name.
Step 2: Compile the Java Program (Compilation)
Command Used
javac Hello.java
What Happens Internally
- javac checks:
-
- Syntax errors
- Type mismatches
- Converts source code into bytecode
- Generates Hello.class
If no errors: compilation succeeds silently.
Common Compilation Errors
- Missing semicolon
- Class name and file name mismatch
- Syntax errors
Example error:
error: ';' expected
Step 3: Execute the Java Program (Execution)
Command Used
java Hello ⚠️ Do not include .class extension
What Happens Internally
- JVM loads Hello.class
- JVM looks for main() method
- JVM executes the program
Output:
Hello Java
Working with Packages (Command Line)
Example Package
package com.softwaretips4u.demo;
public class Test {
public static void main(String[] args) {
System.out.println("Package example");
}
}
Compile with Package Structure
javac -d . Test.java
Resulting Folder Structure:
com/
└── softwaretips4u/
└── demo/
└── Test.class
Execute Packaged Class
java com.softwaretips4u.demo.Test
Using Classpath (-cp / -classpath)
Classpath tells JVM where to find .class files.
java -cp . Test
Or:
java -classpath lib/*;. Test
Why it matters: Essential for running applications with external libraries.
Passing Command-Line Arguments
java Hello one two
args[0] = "one" args[1] = "two"
Compilation vs Execution Errors
Compilation Errors
- Detected by javac
- Syntax and type errors
- Program does not run
Runtime Errors
- Detected by JVM
- Exceptions during execution
Example:
int x = 10 / 0; // Runtime exception
Common Beginner Mistakes
- Running java Hello.java (wrong)
- Including .class during execution
- Not setting classpath
- Wrong directory while executing
- Forgetting -d for packages
Summary Table
| Stage | Command | Output |
|---|---|---|
| Compilation | javac File.java | .class |
| Execution | java ClassName | Program output |
| With package | javac -d . | Folder structure |
| Classpath | -cp | Class resolution |
Interview-Ready Answers
Short Answer
Java programs are compiled using javac and executed using the java command from the command line.
Detailed Answer
In Java, source code is compiled into bytecode using the javac compiler, producing .class files. These files are executed by the JVM using the java command, which loads the class, locates the main() method, and runs the program.
Key Takeaway
Understanding command line compilation and execution clarifies how Java works behind the scenes and is essential for server-side execution, CI pipelines, and debugging.