Skip to content
Preview · sample content · Explore all pages

JDK, JVM and your first program

Last updated: 28 Sept 20263 min read
tutorial
By AITrove Editorial

The JDK contains the compiler and tools for building Java programs; the JVM runs the compiled bytecode.

Working model

Use a supported JDK that matches your project. Check java -version and javac -version in the same terminal. Different versions usually mean PATH points at two installations. Keep the public class name and source filename identical.

Code

Java
public class OrderReceipt {
    public static void main(String[] args) {
        System.out.println("Order received: ORD-1042");
    }
}

Save as OrderReceipt.java. Compile with javac OrderReceipt.java, then run java OrderReceipt. The output is Order received: ORD-1042. Run from the directory containing the compiled class, or supply a classpath.

Cost and design choices

Compilation checks names and types. Execution can still fail because a dependency is missing or an input violates an assumption. Start with this small program before adding an IDE or a build tool.

Separate three failure stages

A compiler error means no valid class file was produced for that compilation. A class-not-found error means the launcher could not locate the requested compiled entry point. A stack trace from main means execution started and the program failed later. Keep the command and working directory in the bug report.

The source filename must match a public top-level class name. Java class names are case-sensitive, so a filename accepted on one filesystem can still hide a portability problem. Run the compiled name without a .class suffix.

Connected lessons

Continue with Java variables and primitive types, Java Tutorial.

Common Mistakes

Running java OrderReceipt.class is incorrect: java expects a class name here. A newer compiler can emit bytecode that an older JVM cannot run; use --release when targeting an older supported runtime.

java
setup
Storage details