Compiling with javac
What you'll learn: How to use the javac command to translate your Java source code into executable bytecode.
Why Compilation Matters
When you write Java code in a .java file, the computer can't run it directly—it's just text. Think of it like writing a recipe in English: a robot chef needs that recipe translated into machine instructions before it can cook. The Java compiler (javac) does this translation work.
From Source to Bytecode
The javac tool takes your .java source file and transforms it into a .class file containing bytecode. Bytecode is a special set of instructions the JVM can understand and execute.
If your source file is named HelloWorld.java, running the compiler produces HelloWorld.class.
How to Use javac
Open your terminal or command prompt in the directory where your Java file lives. Type:
javac HelloWorld.java
If your code has no syntax errors, javac runs silently and creates HelloWorld.class in the same folder. If there are errors (like missing semicolons or typos), the compiler prints error messages pointing you to the problems.
The Compilation Flow
- You write code →
HelloWorld.java - You run
javac HelloWorld.java - Compiler checks syntax and structure
- Success → produces
HelloWorld.class(bytecode) - Now the
javacommand can run that.classfile
Think of compilation as quality control: javac catches mistakes before your program runs, saving you from runtime crashes caused by simple typos.
Key Takeaway: The javac compiler translates your human-readable .java source code into .class bytecode files that the JVM can execute—it's the essential first step in running any Java program.