Before this lesson

This is the starting point for this language; web development is not a prerequisite.

What you will be able to do

Distinguish source code, bytecode and runtime and reproduce a first program.

Understand the mechanism

The JDK includes javac and the java launcher. javac converts .java source into .class bytecode; java starts a virtual machine to execute a class. Saving in an editor does not prove successful compilation.

The conventional entry point is public static void main(String[] args). public permits access, static avoids requiring an instance, void means no return value, and args holds command-line arguments. Understand its role before studying modifiers in depth.

The public class Main must match Main.java, including case. On compilation failure, inspect the first error location. Recompile after fixing it to avoid running a stale .class file.

Runnable example

Use JDK 21. Save Main.java in a separate practice directory, compile with javac -encoding UTF-8 Main.java, then run java Main. Each lesson is independent; do not combine multiple public Main classes.


public class Main {
    public static void main(String[] args) {
        String course = "Java";
        int minutes = 25;
        System.out.println(course + ": " + minutes);
        System.out.println("arguments=" + args.length);
    }
}

Trace and verify

Without arguments the output is Java: 25 and arguments=0. Run java Main one two: the second line becomes arguments=2 without changing source.

Common mistakes and boundaries

Run java Main, without .class. This course explicitly compiles first. If javac is unavailable, install or locate a JDK instead of repeatedly editing valid code.

Practice challenge

Change the course and duration, then remove a semicolon to inspect a compiler error. Restore it and rerun.

Reference solution and reasoning

Changing values affects output. Removing a declaration semicolon causes a syntax error. Restore it, confirm javac succeeds, then run java.

Lab: control where compilation writes and execution reads

When edits do not change terminal output, you may be running an old class file or using the wrong directory. Separate source from compiler output and set the classpath explicitly. Save this as Main.java in a practice directory with JDK 21; run javac -encoding UTF-8 -d out Main.java, then java -cp out Main.

public class Main {
    public static void main(String[] args) {
        String version = "lesson-v2";
        System.out.println(version);
        System.out.println(args.length);
    }
}

Expected observations

Without arguments the output is lesson-v2 and 0. With java -cp out Main HTML CSS, the second line becomes 2.

Why this happens and where it stops applying

javac translates source into bytecode. java -cp out Main loads a class by name from the specified classpath root; Main is not a source-file path. Recompile after changing version. For UnsupportedClassVersionError, compare compiler and runtime versions instead of deleting unrelated code. javac -version and java -version check separate executables; setting JAVA_HOME alone does not prove which executable appears first on PATH.

Extension challenge

Why does adding package study; change the launch command?

Reasoned solution

Compilation with -d out produces out/study/Main.class. Launch java -cp out study.Main: the classpath still names the package-tree root. Using out/study with the unqualified Main confuses the binary class name with a filesystem location.

Further reading