Write and Run Hello World Without an Editor
Introduction
This chapter shows how to create, compile, and run your first Java program without a code editor—only the terminal. You will use javac to compile source code into bytecode and java to run it. This workflow matters on servers, in CI, and whenever GUI tools are unavailable.
Prerequisites
- JDK 21 installed and verified (
java -version,javac -version) - Basic command-line skills (
cd,ls/dir, creating files) - A terminal:
- Windows: PowerShell or Command Prompt
- macOS / Linux: Terminal (zsh or bash)
If installation is not finished, complete the guide for your OS first:
Why Learn Terminal-Only Hello World
Java is not interpreted line-by-line like Python. You compile source files, then run bytecode on the JVM. Practicing in the terminal builds habits you will reuse in build tools, Docker, and automated tests.
Core steps:
- Write a
.javasource file - Compile with
javac→ produces.classbytecode - Run with
java→ JVM executes the bytecode
Step 1: Create a Project Folder
macOS / Linux
mkdir -p ~/java-hello
cd ~/java-helloWindows PowerShell
New-Item -ItemType Directory -Force -Path $HOME\java-hello
Set-Location $HOME\java-helloStep 2: Create HelloWorld.java Without an Editor
The public class name must match the filename. For class HelloWorld, the file must be HelloWorld.java (case-sensitive on Linux and macOS).
macOS / Linux
cat > HelloWorld.java << 'EOF'
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
EOFWindows PowerShell
@'
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
'@ | Set-Content -Path .\HelloWorld.java -Encoding utf8Windows Command Prompt
Command Prompt heredocs are awkward; use PowerShell above, or create the file with Notepad and save as HelloWorld.java in your project folder (choose UTF-8 encoding).
Warning
Avoid rich-text editors that insert “smart quotes” or hidden characters. Broken quotes cause compile errors that confuse beginners.
Understand the Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}| Part | Meaning |
|---|---|
public class HelloWorld | Defines a class named HelloWorld; must match HelloWorld.java |
public static void main(String[] args) | Entry point the JVM calls when you run the program |
System.out.println(...) | Prints a line to standard output (the terminal) |
; | Ends each statement—required in Java |
Unlike Python’s print("Hello") in a .py file, Java wraps code in a class and a main method from the start.
Step 3: Compile with javac
Run this in the folder that contains HelloWorld.java.
javac HelloWorld.javaOn success, you should see a new file HelloWorld.class (bytecode). No output from javac usually means success.
# macOS / Linux — list files
ls -la
# Windows PowerShell
Get-ChildItemStep 4: Run with java
java HelloWorldExpected output:
Hello, World!Tip
Best Practice
Run java HelloWorld, not java HelloWorld.class or java HelloWorld.java. The java command expects the class name, not the filename with extension.
Platform Command Cheat Sheet
| Step | macOS / Linux | Windows (PowerShell, same folder) |
|---|---|---|
| Compile | javac HelloWorld.java | javac HelloWorld.java |
| Run | java HelloWorld | java HelloWorld |
| List output | ls *.class | Get-ChildItem *.class |
Common Errors and Fixes
error: class HelloWorld is public, should be declared in a file named HelloWorld.java
- Cause: Class name and filename differ (including capitalization).
- Fix: Rename the file to match the public class exactly.
javac: command not found
- Cause: JDK not installed or
PATHmissing$JAVA_HOME/bin. - Fix: Revisit the install chapter for your OS.
Could not find or load main class HelloWorld
- Cause: Wrong directory, typo in class name, or compile step failed.
- Fix: Run
javacagain from the folder that containsHelloWorld.class, thenjava HelloWorld.
java.lang.UnsupportedClassVersionError
- Cause:
.classfiles were compiled with a newer JDK than thejavaon yourPATH. - Fix: Align
javaandjavacversions (java -versionandjavac -versionshould both show 21).
What’s Next
You have completed the compile-run cycle. Next, install an editor for syntax highlighting and completion: Install a Code Editor, then run Hello World inside IntelliJ IDEA.
FAQ
Why does Java need a main method?
The JVM needs a defined entry point. public static void main(String[] args) is the standard entry for command-line applications.
Can I put multiple classes in one .java file?
Only one public top-level class per file, and its name must match the filename. Additional non-public classes in the same file are allowed but uncommon in beginners’ code.
What is the difference between javac and java?
javac compiles source to bytecode (.class). java runs bytecode on the JVM.
Do I need an editor to learn Java?
No for this exercise—but an editor (IntelliJ IDEA or VS Code) becomes valuable quickly for larger programs.
Can I delete .class files?
Yes. They are build outputs; javac can regenerate them from your .java source anytime.