Skip to content
AITroveRead. Build. Understand.
Make this comfortable

JDK, JVM and your first program

Last updated: 29 Sept 20264 min read
tutorial
By AITrove Editorial

A Java toolchain compiles source with javac and runs bytecode with a compatible Java runtime.

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.

Separate toolchain failures from program failures

Check both java -version and javac -version before investigating a program. A machine can launch one installed runtime while its compiler comes from another installation. Keep the compiler release target separate from the compiler installation; javac --release 8 on a newer JDK checks the stated platform API and language boundary.

Compile into a dedicated output directory, then give that directory to the class path. Launch a packaged class by its full binary name, not by a source-file pathname. A missing class and an exception thrown from main occur at different stages; preserving the actual error makes that distinction visible.

The small program verifies compilation and launch. It does not validate an IDE’s test configuration, a dependency resolver or deployment packaging. Once the command-line build works, reproduce the same source roots, release target and runtime selection in the IDE.

java
setup
Storage details