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

Compiling with javac

Using the Java compiler (javac) to transform .java source files into .class bytecode files.

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

  1. You write code → HelloWorld.java
  2. You run javac HelloWorld.java
  3. Compiler checks syntax and structure
  4. Success → produces HelloWorld.class (bytecode)
  5. Now the java command can run that .class file

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.