From .java file to running program
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A Java program runs in two steps. javac turns your .java source file into bytecode stored in .class files, and java starts a JVM (Java Virtual Machine) that loads that bytecode and runs it.
javac compiles, java runs
Bytecode is the set of instructions in a .class file that javac produces from Java source; the JVM reads and runs it on any operating system.
javac is the Java compiler. It reads a .java file, checks it for errors and writes one .class file for every class in it. A .class file holds bytecode: compact instructions for the JVM, not for your CPU.
javac FeeReceipt.java # writes FeeReceipt.classjava FeeReceipt # starts a JVM, loads the class, calls mainjava is the launcher. It starts a JVM, asks it to load the class you named and calls that class's main method. So you give java a class name (java FeeReceipt), not a file name.
Example 1 · Compile and run a fee receipt
public class FeeReceipt { public static void main(String[] args) { String student = "Riya Sharma"; int tuition = 45000; int hostel = 18000; int total = tuition + hostel; System.out.println("Receipt for " + student); System.out.println("Tuition: Rs " + tuition); System.out.println("Hostel: Rs " + hostel); System.out.println("Total: Rs " + total); }}Receipt for Riya Sharma Tuition: Rs 45000 Hostel: Rs 18000 Total: Rs 63000
What this code does
javac FeeReceipt.javawritesFeeReceipt.classnext to the source.java FeeReceiptstarts a JVM, loads that class and callsmain.- The
.javafile is not needed to run the program. You could send onlyFeeReceipt.classto a friend with a JVM and it would print the same receipt.
Example 2 · Common launch mistakes and their real errors
javac --release 17 FeeReceipt.javajava FeeReceipt.classjavac FeeReceiptjava feereceiptjava FeeReceipt.java$ javac --release 17 FeeReceipt.java $ java FeeReceipt.class Error: Could not find or load main class FeeReceipt.class Caused by: java.lang.ClassNotFoundException: FeeReceipt.class $ javac FeeReceipt error: Class names, 'FeeReceipt', are only accepted if annotation processing is explicitly requested 1 error $ java feereceipt Error: Could not find or load main class feereceipt Caused by: java.lang.NoClassDefFoundError: feereceipt (wrong name: FeeReceipt) $ java FeeReceipt.java Receipt for Riya Sharma Tuition: Rs 45000 Hostel: Rs 18000 Total: Rs 63000
What this code does
javawants a class name, sojava FeeReceipt.classlooks for a class literally calledFeeReceipt.classand fails.javacwants a file name, sojavac FeeReceiptfails too.- Class names are case-sensitive:
feereceiptfinds the file on Windows (its file names ignore case) but the class inside isFeeReceipt, hence "wrong name". The last command uses source-file mode (Java 11 and later) and runs the program directly.
Since Java 11, java FeeReceipt.java also works for a program in one file. It compiles the source in memory and runs it, without writing a .class file. That is handy for small scripts; real projects still compile first.
A simplified model of the JVM
A JVM is a program that runs bytecode. The model below has four parts. It is a simplified picture for learning, not the exact design of any one JVM. HotSpot, the JVM inside Oracle JDK and most OpenJDK builds, follows it closely.
Example 3 · The JVM parts at work
import java.util.List;
class Ticket { String passenger; int fare;
Ticket(String passenger, int fare) { this.passenger = passenger; this.fare = fare; }}
public class JvmPartsDemo { static void book(String name, int fare) { Ticket t = new Ticket(name, fare); // the object lives on the heap printStack(); System.out.println("Booked " + t.passenger + " for Rs " + t.fare); }
static void printStack() { List<String> frames = StackWalker.getInstance() .walk(s -> s.map(StackWalker.StackFrame::getMethodName).toList()); System.out.println("Methods on the stack (top first): " + frames); }
public static void main(String[] args) { System.out.println("JvmPartsDemo loaded by: " + JvmPartsDemo.class.getClassLoader().getName()); System.out.println("Ticket loaded by: " + Ticket.class.getClassLoader().getName()); System.out.println("String loaded by: " + String.class.getClassLoader()); book("Asha", 450); }}JvmPartsDemo loaded by: app Ticket loaded by: app String loaded by: null Methods on the stack (top first): [printStack, book, main] Booked Asha for Rs 450
What this code does
- Class loader: your classes were loaded by the application class loader (named
appsince Java 9). Core classes such asStringcome from the bootstrap loader, which Java reports asnull. - Stack and heap:
StackWalker(Java 9) lists the method calls on this thread's stack, newest first. TheTicketobject created inbooklives on the heap; the variabletthat points to it lives inbook's stack frame.
Each part gets its own module later in this section. For now, learn the name and the one job of each part.
- Class loader: finds
.classfiles (in your folders, inside JAR files and inside the JDK), checks them and loads each class the first time it is needed. - Runtime data areas (memory): the heap holds objects, each thread has its own stack of method calls and local variables, and the method area holds class data such as method bytecode.
- Execution engine: runs the bytecode. HotSpot first interprets it, then compiles busy methods to machine code with a JIT (just-in-time) compiler.
- Garbage collector: frees heap memory used by objects that the program can no longer reach.
Order of execution
What happens when you run java FeeReceipt
Tap the steps in the order they happen.
Why bytecode makes Java portable
Portable means the same compiled program runs on different computers without changes. A .class file does not contain Windows or Linux machine code. It contains bytecode, which every JVM understands in the same way.
Example 4 · Same bytecode, different computer
public class PlatformDemo { public static void main(String[] args) { System.out.println("Operating system: " + System.getProperty("os.name")); System.out.println("CPU type: " + System.getProperty("os.arch")); System.out.println("Java version: " + System.getProperty("java.version"));
int marks = 78 + 85 + 91; System.out.println("Total marks: " + marks); }}Operating system: Windows 11 CPU type: amd64 Java version: 21.0.7 Total marks: 254
What this code does
- This run was on Windows 11 with a Java 21 JVM. Copy the same
PlatformDemo.classto a Linux or macOS machine with Java 17 or later and the first three lines change, because they come from the JVM and the computer it runs on. - The last line is the same everywhere: the bytecode for
78 + 85 + 91means the same thing on every JVM. That is what "portable" means.
The JVM itself is not portable: there is a different JVM build for Windows, macOS and Linux, and for x64 and ARM chips. Each one turns the same bytecode into the right machine code for its computer. This is the idea behind Java's old slogan "write once, run anywhere". There is one limit: the JVM must be new enough. A class compiled for Java 21 does not run on a Java 17 JVM. The next lesson shows the error and the fix.
JDK, JRE and JVM: a quick refresher
The JVM runs bytecode. The JRE (Java Runtime Environment) is a JVM plus the Java class library, such as String and ArrayList: enough to run programs. The JDK (Java Development Kit) is the runtime plus developer tools such as javac, jar, javap and jcmd.
Example 5 · Check which JDK you are using
java -versionjavac -versionopenjdk version "21.0.7" 2025-04-15 OpenJDK Runtime Environment JBR-21.0.7+9-895.130-jcef (build 21.0.7+9-b895.130) OpenJDK 64-Bit Server VM JBR-21.0.7+9-895.130-jcef (build 21.0.7+9-b895.130, mixed mode, sharing) javac 21.0.7
What this code does
- This run used a JDK 21 build from JetBrains (JBR). An Oracle or Eclipse Temurin JDK prints its own vendor name, and your version number may differ.
- If
java -versionworks butjavac -versionsays "not recognized" or "command not found", you have only a runtime on your PATH, not a JDK.
Since Java 11, Oracle and OpenJDK publish only the JDK; some vendors, such as Eclipse Adoptium, still offer JRE builds. As a developer you install a JDK, and java -version and javac -version tell you which one is on your PATH.
Next step
Check your understanding
11 quick questions