How a Java Program Compiles and Runs
Where you are: Week 0 review > How a Java program compiles and runs
Try This First
A folder holds two files after a classmate builds a program: Hello.java and Hello.class. One was typed by a person; the other was produced by a tool. Before reading on, decide which is which.
Reveal
Hello.java is the source a person wrote. Hello.class is bytecode a tool produced from it. You edit the .java file and never touch the .class file by hand.
Before You Start
Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.
What You Need To Walk In With
Walk into the next class able to state these:
- Source code is for people; bytecode is for the machine. You write
.java; the Java Virtual Machine (JVM) runs.class. - The compiler
javacreads.javaand produces.class. The launcherjavaruns the.class. - The cycle is edit, compile, run. Change the source, and you must compile again before the change takes effect.
- A syntax error is caught at compile time (no
.classis produced). A runtime error appears while the program runs. A logic error is wrong output with no crash.
You should be able to: name the file each tool produces or consumes, and say at which stage a given error is caught.
How It Works
A .java file is text a person can read. The computer cannot run it directly. The compiler (javac) reads the source, checks it against the rules of the language, and writes a second file, .class, which holds bytecode: instructions for the JVM rather than for one specific processor.
Hello.java --javac--> Hello.class --java--> output
(you write) (bytecode) (what runs)
^ |
| edit and save | wrong output? fix the source
+------------------------+
Running java Hello executes whatever Hello.class is on disk. If you change Hello.java and skip the compile step, the old .class runs and the change does not appear. An editor such as VS Code or IntelliJ runs javac for you when you press Run, which hides the step but does not remove it.
The compile step exists for two reasons:
- Early checking: a misspelled name or a type mismatch stops
javacfrom producing a.class, so the mistake surfaces before the program ever starts. - Portability: the same
.classbytecode runs on any machine that has a JVM, whether Windows, macOS, or Linux.
The kit you install is the JDK (Java Development Kit). It contains javac (to compile) plus the JRE (Java Runtime Environment, enough to run but not compile). This course installs the full JDK.
Three kinds of error
| Kind | Caught | What happened |
|---|---|---|
| Syntax | Compile time | A grammar rule was broken (a missing semicolon, a misspelled keyword). No .class is produced. |
| Runtime | While running | The program started, then failed (for example, dividing by zero). |
| Logic | Never automatically | The program ran and produced output, but the output is wrong. |
A compiler message names the file, the line, the problem, and a caret at the column:
Hello.java:4: error: ';' expected
System.out.println("hi")
^
Fix the lowest-numbered error first, then compile again, because one mistake often produces several messages.
Worked Example: Predict, Then Check
public class Hello {
public static void main(String[] args) {
System.out.println("Year: 2026");
}
}
You compile with javac Hello.java, then run java Hello. Predict the output before reading on.
Reveal
Year: 2026
javac produces Hello.class; java Hello runs it and prints one line. Change the text in the source and you must run javac again, or the old line prints.
A Common Mistake
A frequent first-week belief is that the .java file is what the computer executes, so editing the source and re-running should be enough. The JVM runs .class bytecode, not source. When a change does not seem to take effect, the usual cause is a skipped compile step: the old .class is still on disk. Recompile, then run. (Source: BJP (Reges and Stepp), Ch 1; JLS 25, §12.1, Java Virtual Machine Startup.)
Go Deeper (optional)
For the curious: bytecode is not machine code for any real processor. It is an instruction set for an abstract machine, and the JVM translates it to the host processor as the program runs (just-in-time compilation). That extra layer is what allows one .class file to run unchanged on very different hardware. None of this is needed to write programs, but it explains why Java describes itself as “write once, run anywhere.”
Check Yourself
Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.
Check your understanding
When you run a Java program, which file does the JVM actually execute?
What does the javac command produce?
Which kit allows you to compile Java, not just run it?
You edit Hello.java but run java Hello without recompiling. Which version runs?
A missing semicolon is caught at which stage?