Skip to content

VOL 01 / CH 01 / LESSON 01

1.1 Hello World, Your First Program ​

Prerequisites: Ability to open a terminal and navigate into a directory Estimated time: 20 minutes Practice environment: JDK 17 or higher and Bash; traditional Java 17 entry point syntax You are ready to continue when: You can independently compile and run HelloWorld.java

Get the machine to print a greeting first. The terminology can follow.

1. Confirm JDK Availability ​

Run the following commands in the terminal:

bash
javac --version
java --version

Both commands should print version information, such as:

text
javac 17.0.x
openjdk 17.0.x ...

The specific vendor, patch level, and formatting of the second line may vary. Here, we need to verify two things:

  • javac is present: it compiles .java source files into JVM-readable .class bytecode files;
  • java is present: it starts the Java Virtual Machine, loads, and executes classes.

If the terminal indicates that the commands are not found, first follow the JDK Installation Guide for dev.java to install the JDK, then reopen the terminal. Installing only a runtime may leave you without javac. Use the same JDK for compilation and execution at first; an older JVM may reject bytecode produced with a newer compiler’s defaults.

2. Build a Clean Practice Directory ​

bash
chapter1_workdir=$(mktemp -d)
readonly chapter1_workdir
mkdir -p "$chapter1_workdir/hello-java"
cd "$chapter1_workdir/hello-java"

Create HelloWorld.java using any plain text editor. Do not save it as HelloWorld.java.txt.

java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

The case of the filename must exactly match the public class HelloWorld.

3. Compilation ​

First, create the output directory, then compile:

bash
mkdir -p out
javac -d out HelloWorld.java

When successful, javac typically prints nothing. Check the output:

bash
find out -type f -name '*.class' -print

You should see:

text
out/HelloWorld.class

Keeping source and compiled files in separate directories makes them easier to inspect and reduces the chance of running a stale .class file.

4. Running ​

bash
java -cp out HelloWorld

Expected output:

text
Hello World!

The -cp out tells the launcher to look for the class in the out directory. The argument passed to java is the class name HelloWorld, not the filename, so do not write java -cp out HelloWorld.class.

text
Source file HelloWorld.java
        │ javac -d out
        ▼
Bytecode out/HelloWorld.class
        │ java -cp out HelloWorld
        ▼
JVM loads and executes the class, output appears in the terminal

5. After modifying code, you must recompile ​

Change the output to:

java
System.out.println("Hello from the programming-basics section!");

Then repeat:

bash
javac -d out HelloWorld.java
java -cp out HelloWorld

If only the second command is run, the JVM will still execute the bytecode from the previous compilation. Modifications to the source files will not automatically be reflected in the .class file.

An Optional Shortcut ​

Since Java 11, the launcher supports running a single source file directly:

bash
java HelloWorld.java

This is suitable for one-off small programs. The current exercise still follows the explicit workflow of javac followed by java, as it allows you to clearly see the compiled output and better mirrors the build process of future multi-file projects. Do not mix these two commands in the same debugging session.

Differences from Python and C++ ​

Python commonly lacks a user-initiated, standalone build command:

bash
python3 hello.py

This does not mean Python "completely avoids compilation." For example, CPython first compiles source code into bytecode, which is then executed by the virtual machine, though this process is handled automatically by the runtime command.

C++ typically compiles and links source code directly into a native executable for the current platform:

bash
g++ hello.cpp -o hello
./hello

Java's .class file is designed for compatibility with the JVM; C++ outputs are generally influenced by the operating system, CPU architecture, ABI, and dependencies. Both languages can produce portable source code, but their resulting executables run under different conditions.

Quick Troubleshooting ​

SymptomFirst Check
javac: command not foundIs JDK installed? Does the new terminal's PATH include JDK's bin
Cannot find HelloWorld.javaIs the current directory correct? Verify using pwd and ls
class HelloWorld is public...Is the filename exactly HelloWorld.java
Could not find or load main classDid the compilation succeed? Does -cp out point to the artifacts directory? Does the capitalization match?
Output still shows old textDid you re-run javac after making changes?

Before Closing ​

Confirm that both version commands are available, that out/HelloWorld.class exists, and that changes to the text only take effect after recompilation. Then clean up the exercise directory:

bash
cd
ls -ld -- "$chapter1_workdir"
rm -r -- "$chapter1_workdir"

You now have four things to check: source, compilation command, bytecode, and output. Larger projects use more tools, but debugging still means checking each step.

Next, we’ll work through what each line does.

→ Next: Understanding Java Code Line by Line

References: Java 17 java Launcher Documentation, Java 17 javac Compiler Documentation.

Built with VitePress | Software Systems Atlas