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:
javac --version
java --versionBoth commands should print version information, such as:
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:
javacis present: it compiles.javasource files into JVM-readable.classbytecode files;javais 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
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.
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:
mkdir -p out
javac -d out HelloWorld.javaWhen successful, javac typically prints nothing. Check the output:
find out -type f -name '*.class' -printYou should see:
out/HelloWorld.classKeeping source and compiled files in separate directories makes them easier to inspect and reduces the chance of running a stale .class file.
4. Running
java -cp out HelloWorldExpected output:
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.
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 terminal5. After modifying code, you must recompile
Change the output to:
System.out.println("Hello from the programming-basics section!");Then repeat:
javac -d out HelloWorld.java
java -cp out HelloWorldIf 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:
java HelloWorld.javaThis 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:
python3 hello.pyThis 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:
g++ hello.cpp -o hello
./helloJava'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
| Symptom | First Check |
|---|---|
javac: command not found | Is JDK installed? Does the new terminal's PATH include JDK's bin |
Cannot find HelloWorld.java | Is 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 class | Did the compilation succeed? Does -cp out point to the artifacts directory? Does the capitalization match? |
| Output still shows old text | Did 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:
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.