javac vs java: The Compiler and Runtime Tools
What you'll learn: You'll understand the distinct roles of javac (the compiler) and java (the runtime tool) in transforming and executing your Java programs.
Two Tools, Two Jobs
Think of building a house: you need both a construction crew to build it and residents to live in it. In Java, javac is your construction crew, and java is what makes your program come alive.
javac: The Compiler
javac is the Java compiler. Its job is to translate your human-readable .java source files into .class files containing bytecode—instructions the JVM can understand.
What it does:
- Reads your
.javafile - Checks for syntax errors and compilation problems
- Produces a
.classfile if everything is correct
Example command:
javac HelloWorld.java
This creates HelloWorld.class (the bytecode file).
java: The Runtime Tool
java is the Java launcher. It starts the JVM and tells it to execute the bytecode in your .class file.
What it does:
- Starts the JVM
- Loads the specified
.classfile - Executes the
mainmethod, running your program
Example command:
java HelloWorld
Notice: no .class extension needed—you specify the class name, not the filename.
The Sequential Relationship
You always compile first, then run:
javactransforms source → bytecodejavaexecutes bytecode on the JVM
You can't run a .java file directly with java, and javac doesn't execute anything—it only prepares your code for execution.
Key Takeaway: javac compiles your .java source into .class bytecode, while java launches the JVM to execute that bytecode. They're separate but sequential steps in running any Java program.