Course contentsShow
← Java
Lesson 22 of 2,8701. Java Setup and First ProgramFree lesson

javac vs java: The Compiler and Runtime Tools

Distinguish between javac (compiling .java to .class) and java (executing .class files on the JVM).

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 .java file
  • Checks for syntax errors and compilation problems
  • Produces a .class file 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 .class file
  • Executes the main method, 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:

  1. javac transforms source → bytecode
  2. java executes 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.