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:

  1. Write a .java source file
  2. Compile with javac → produces .class bytecode
  3. Run with java → JVM executes the bytecode

Step 1: Create a Project Folder

macOS / Linux

bash
mkdir -p ~/java-hello
cd ~/java-hello

Windows PowerShell

powershell
New-Item -ItemType Directory -Force -Path $HOME\java-hello
Set-Location $HOME\java-hello

Step 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

bash
cat > HelloWorld.java << 'EOF'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
EOF

Windows PowerShell

powershell
@'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
'@ | Set-Content -Path .\HelloWorld.java -Encoding utf8

Windows 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

java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
PartMeaning
public class HelloWorldDefines 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.

bash
javac HelloWorld.java

On success, you should see a new file HelloWorld.class (bytecode). No output from javac usually means success.

bash
# macOS / Linux — list files
ls -la
 
# Windows PowerShell
Get-ChildItem

Step 4: Run with java

bash
java HelloWorld

Expected output:

text
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

StepmacOS / LinuxWindows (PowerShell, same folder)
Compilejavac HelloWorld.javajavac HelloWorld.java
Runjava HelloWorldjava HelloWorld
List outputls *.classGet-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 PATH missing $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 javac again from the folder that contains HelloWorld.class, then java HelloWorld.

java.lang.UnsupportedClassVersionError

  • Cause: .class files were compiled with a newer JDK than the java on your PATH.
  • Fix: Align java and javac versions (java -version and javac -version should 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.