GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Single-file execution | java.evolved

Home / Tooling / Single-file execution
Tooling Beginner

Single-file execution

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Run single-file Java programs directly without javac.

Tooling
Code Comparison
✕ Java 8
$ javac HelloWorld.java
$ java HelloWorld
// Two steps every time
✓ Java 11+
$ java HelloWorld.java
// Compiles and runs in one step
// Also works with shebangs:
#!/usr/bin/java --source 25
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
⚡

One command

java File.java compiles and runs in one step.

📝

Script-like

Add a shebang line to make .java files executable scripts.

🎓

Learning-friendly

Newcomers run code immediately without learning build tools.

Old Approach
Two-Step Compile
Modern Approach
Direct Launch
Since JDK
11
Difficulty
Beginner
JDK Support
Single-file execution
Available

Widely available since JDK 11 (Sept 2018)

How it works

The Java launcher can compile and run a single source file in one command. Combined with shebang support on Unix, Java files can work as scripts. No separate compilation step needed.

Related Documentation
Launch Single-File Source-Code Programs (JEP 330) ↗
Related patterns
Tooling Intermediate

Multi-file source launcher

Java 8
$ javac *.java
$ java Main
// Must compile all files first
// Need a build tool for dependencies
Java 22+
$ java Main.java
// Automatically finds and compiles
// other source files referenced
// by Main.java
Hover to see modern ➜
JDK 22+ →
Tooling Advanced

Compact object headers

Java 8
// Default: 128-bit object header
// = 16 bytes overhead per object
// A boolean field object = 32 bytes!
// Mark word (64) + Klass pointer (64)
Java 25
// -XX:+UseCompactObjectHeaders
// 64-bit object header
// = 8 bytes overhead per object
// 50% less header memory
// More objects fit in cache
Hover to see modern ➜
JDK 25+ →
Tooling Intermediate

JFR for profiling

Java 8
// Install VisualVM / YourKit / JProfiler
// Attach to running process
// Configure sampling
// Export and analyze
// External tool required
Java 9+
// Start with profiling enabled
$ java -XX:StartFlightRecording=
    filename=rec.jfr MyApp

// Or attach to running app:
$ jcmd <pid> JFR.start
Hover to see modern ➜
JDK 9+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.