SECTION 1
Historical Context & Motivation
Before fancy code editors and IDEs like IntelliJ or Eclipse existed, every programmer compiled and ran their code from a command line — a simple text-based interface where you type instructions directly to the operating system. When James Gosling and his team at Sun Microsystems created Java in the mid-1990s, the command-line tools javac and java were the very first way anyone ever compiled and ran a Java program. Understanding these tools gives you direct control over the compilation process and helps you grasp what IDEs do behind the scenes.
1991 • Project Green Begins
1995 • Java 1.0 Released
javac compiler and java runtime launcher. The motto is "Write Once, Run Anywhere."2006 • Java Goes Open Source
2017–Now • Modern JDK Releases
javac and java commands remain the fundamental tools in every JDK distribution, even as IDEs have become popular.Even today, every IDE you might use — IntelliJ IDEA, Eclipse, or VS Code with extensions — ultimately calls javac and java under the hood. So the question is: how exactly do you turn a plain .java text file into a running program using just these two commands?
SECTION 2
Core Principles & Definitions
Java uses a two-step process to go from the code you write to a running application. First, your human-readable source code is compiled into an intermediate form. Then, that intermediate form is executed by the Java Virtual Machine. This design is what makes Java portable across different operating systems.
Source Code (.java)
.java extension, and the filename must match the public class name inside the file.Bytecode (.class)
javac. Bytecode is not human-readable — it is designed for the JVM to interpret.javac (Compiler)
.java file, checks for syntax errors, and produces a .class file containing bytecode.java (Launcher)
.class file, and begins executing the main method.JVM (Java Virtual Machine)
KEY TAKEAWAY
.java file is the script written in English. The javac compiler is like a translator who converts the script into a universal stage notation (bytecode) that any theater company in the world can read. The java launcher is the director who reads that notation and makes the actors (your CPU) perform the show.SECTION 3
Visual Explanation — The Compilation Pipeline
javac, which produces bytecode. The java launcher then loads that bytecode into the JVM and produces your program's output. The terminal commands at the bottom show the exact keystrokes you type.Notice in the diagram that there are two distinct phases. The compilation phase (using javac) is where errors in your syntax — like a missing semicolon or a misspelled keyword — are caught. If javac finds any errors, it will print them to the terminal and refuse to create the .class file. Only when compilation succeeds can you move to the execution phase (using java), where the JVM actually runs your program.
SECTION 4
How It Works — Step by Step
Writing the Source File
Every Java program begins as a plain text file saved with the .java extension. A critical rule is that the filename must exactly match the public class name declared inside the file, including capitalization. For example, if your class is called HelloWorld, the file must be named HelloWorld.java. You can create this file in any text editor — Notepad, VS Code, nano, or vim.
Common Mistake
helloworld.java (lowercase) but the class inside is public class HelloWorld, the compiler will throw an error. Java is case-sensitive — capitalization matters everywhere.Compiling with javac
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux), navigate to the folder containing your .java file using the cd command, and type:
COMPILE COMMAND
javac = the Java compiler program; HelloWorld.java = the source file to compile. If successful, a file called HelloWorld.class appears in the same directory. If there are errors, they are printed with line numbers.
Running with java
Once compilation succeeds, you execute the program by typing:
RUN COMMAND
java = the JVM launcher; HelloWorld = the class name (NOT the filename — no .class extension). The JVM finds the public static void main(String[] args) method and begins execution there.
Important
java, you provide the class name, not the file name. Writing java HelloWorld.class is a very common beginner error and will produce an error message about not finding the class.SECTION 5
Understanding Compiler Errors & the Classpath
One of the biggest advantages of command-line compilation is that you see error messages directly in the terminal. Learning to read these messages is a crucial skill. The compiler tells you the file name, the line number, and a description of what went wrong. Let's look at the anatomy of a typical error and the concept of the classpath.
javac error into its parts: the file name, line number, error type, description, and the caret pointer showing exactly where the problem is.The Classpath
The classpath tells the JVM where to look for .class files. By default, it looks in the current directory. If your class files are somewhere else, you can specify the classpath using the -cp flag. For example, java -cp /myproject/bin HelloWorld tells Java to look inside the /myproject/bin folder for the compiled class. For now, keeping your terminal in the same folder as your files is the simplest approach.
SECTION 6
Worked Example — Hello World from Scratch
Let's walk through the complete process of writing, compiling, and running a Java program from the command line. We will create a program that prints a greeting message.
Compiling and Running HelloWorld
- 1
Step 1 — Create the Source File
Open any text editor and type the following code. Save the file asHelloWorld.javain a folder you can easily navigate to (for example,C:\JavaProjectson Windows or~/JavaProjectson macOS/Linux).public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } - 2
Step 2 — Open the Terminal and Navigate
Open your terminal application. Use thecd(change directory) command to navigate to the folder where you saved the file.cd ~/JavaProjects(macOS/Linux) orcd C:\JavaProjects(Windows) - 3
Step 3 — Verify the JDK is Installed
Typejavac -versionto confirm that the Java Development Kit is installed and available on your system PATH. You should see output likejavac 17.0.2. If you get a "command not found" error, you need to install the JDK and add it to your system's PATH environment variable.javac -version→javac 17.0.2 - 4
Step 4 — Compile the Program
Run the compiler by typingjavac HelloWorld.java. If the code has no errors, the terminal will simply return to the prompt with no output — silence is success! A new file calledHelloWorld.classwill appear in the same directory. You can verify by typingls(macOS/Linux) ordir(Windows).javac HelloWorld.java→ No output (success).HelloWorld.classcreated. - 5
Step 5 — Run the Program
Execute the compiled bytecode withjava HelloWorld. Note: do NOT include the.classextension. The JVM locates themainmethod and starts executing it.java HelloWorld→ Hello, World!
Pro Tip
java HelloWorld without recompiling, you will still see the old program's behavior because the .class file hasn't changed.SECTION 7
Command Line vs. IDE — Strengths & Limitations
You might wonder: if IDEs exist and make life easier, why bother with the command line? The truth is that both approaches have real value. Understanding the CLI gives you foundational knowledge that pays off when debugging IDE issues, working on servers, or automating builds. Meanwhile, IDEs dramatically speed up everyday development work.
| Feature | Command Line (javac / java) | IDE (IntelliJ, Eclipse, etc.) |
|---|---|---|
| Setup | Just install the JDK — nothing else needed | Install JDK plus the IDE application |
| Error Feedback | Errors shown after you compile | Errors highlighted in real-time as you type |
| Speed for Small Programs | Very fast — no project setup required | Requires creating a project first |
| Large Projects | Gets tedious managing many files manually | Excellent — auto-manages dependencies and builds |
| Learning Value | High — you see exactly what happens at each step | Lower — the process is hidden behind buttons |
| Server / Remote Use | Essential — servers rarely have graphical interfaces | Not available on remote servers without GUI |
Comparing command-line and IDE workflows for Java development
KEY TAKEAWAY
SECTION 8
Connection to Build Tools & Advanced Workflows
As your projects grow beyond a handful of files, manually running javac on each file becomes impractical. This is where build tools come in. Tools like Maven and Gradle automate the compilation, testing, and packaging of Java projects. But at their core, these tools are calling javac and java for you — just in a smarter, more organized way.
| Concept | What You Know Now | What Comes Next |
|---|---|---|
| Compilation | Run javac manually on each file | Build tools compile all files automatically based on a config file |
| Dependencies | Set the classpath manually with -cp | Maven/Gradle download and manage library dependencies for you |
| Packaging | Individual .class files | Bundle into .jar (Java Archive) files for easy distribution |
| Testing | Test by running the program and checking output visually | Automated testing frameworks like JUnit run thousands of tests in seconds |
How basic CLI skills scale into professional Java workflows
Starting with JDK 11, Java also supports single-file source-code launching. You can skip the explicit compile step and run java HelloWorld.java (note the .java extension), and the JVM will compile and run the file in one step. This is handy for quick experiments, but for multi-file projects, the traditional two-step process remains essential. As you advance in your Java journey, the command-line fundamentals you learn today will make every new tool easier to understand.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
javac then java) instead of directly running the source code like some other languages do. What advantage does the intermediate bytecode provide?Show answer
javac compiler converts source code into bytecode, which is a platform-neutral set of instructions. This bytecode can then be run on any operating system (Windows, macOS, Linux) that has a JVM installed, without recompiling. This is Java's "Write Once, Run Anywhere" philosophy. If Java ran source code directly, it would need a different interpreter optimized for each platform, losing the portability advantage that bytecode provides.Question 2: BASIC CALCULATION
Greeting.java containing a class called public class Greeting with a valid main method. Write the exact two terminal commands needed to compile and run this program.Show answer
javac Greeting.java — this compiles the source file and produces Greeting.class. (2) java Greeting — this runs the program by starting the JVM and loading the Greeting class. Notice that the compile command uses the full filename with the .java extension, but the run command uses only the class name without any extension.Question 3: INTERMEDIATE
MyApp.java:
public class myApp {
public static void main(String[] args) {
System.out.println("It works!")
}
}
Identify all the errors that javac would report and explain how to fix each one.Show answer
myApp (lowercase 'm') does not match the filename MyApp.java (uppercase 'M'). Java requires an exact match, so either rename the file to myApp.java or change the class declaration to public class MyApp. Second, the System.out.println line is missing a semicolon at the end. Every statement in Java must end with a semicolon. The corrected line is System.out.println("It works!");. Always fix the first reported error first and recompile, as later errors are sometimes caused by earlier ones.Question 4: APPLIED
java.time.LocalDate.now().Show answer
nano DatePrinter.java (or vim). Inside, write: import java.time.LocalDate; public class DatePrinter { public static void main(String[] args) { System.out.println(LocalDate.now()); } }. Save and exit the editor. Then compile with javac DatePrinter.java. If no errors appear, run with java DatePrinter. The output will be something like 2025-01-15. This scenario demonstrates exactly why command-line skills matter — on remote servers, the CLI is often your only option.Question 5: CRITICAL THINKING
.class file. However, when they run java Calculator, they get: Error: Main method not found in class Calculator. The code compiles without any warnings. What could cause this runtime error despite successful compilation, and what are at least two possible fixes?Show answer
public static void main(String[] args). Compilation can succeed because a class doesn't need a main method to be valid Java — only the JVM requires it as an entry point. Possible causes include: (1) the method signature is wrong — for example, public void main(String[] args) missing static, or public static void main(String args) missing the brackets; (2) the method is not public; (3) the return type is int instead of void. The fix is to ensure the method signature is exactly public static void main(String[] args) — every keyword matters.SUMMARY
Lesson Summary
Summary
Java programs are built through a two-step process. First, you use the javac compiler to convert your human-readable .java source file into platform-independent bytecode (.class file). Then, you use the java launcher to start the JVM (Java Virtual Machine), which interprets the bytecode and runs your program. The filename must match the public class name, you include the .java extension when compiling but omit the .class extension when running.
Mastering command-line compilation gives you a transparent view of how Java works under the hood — knowledge that transfers to understanding IDEs, build tools like Maven and Gradle, and real-world server deployments. Remember: reading compiler error messages carefully (file, line number, description, caret) is one of the most valuable debugging skills you can develop.