惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
The Cloudflare Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Jina AI
Jina AI
V
V2EX
罗磊的独立博客
V
Visual Studio Blog
A
About on SuperTechFans
IT之家
IT之家
P
Proofpoint News Feed
B
Blog
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How the Java Launcher Works — Running Programs Without Co...
Md Jamilur Rahman · 2026-06-19 · via DEV Community

Md Jamilur Rahman

You know the drill. Write code, compile it, run it. Write code, compile it, run it. Over and over. What if you could skip the middle step entirely?

Since JDK 11, Java lets you run a source file directly — no separate javac step needed. The java launcher handles compilation behind the scenes. And as of JDK 22, this works with multi-file programs too.

Let me walk through how it works, when to use it, and where it breaks down.

Single-File Source-Code Programs

Create a file called HelloWorld.java:

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

Instead of running javac HelloWorld.java followed by java HelloWorld, you just run:

$ java HelloWorld.java

That's it. The launcher compiles the file in memory and runs it. No .class file gets written to disk.

This works because the java launcher automatically invokes the Java compiler when you pass it a .java file. The compiled bytecode lives in memory for the duration of the program.

Passing Arguments

You can pass arguments the same way you would with a compiled class:

public class HelloJava {
    public static void main(String[] args) {
        IO.println("Hello " + args[0]);
    }
}

$ java HelloJava.java World!

Output:

Hello World!

Nothing special here — the launcher forwards arguments to your main method just like normal.

Multiple Classes in One File

Sometimes you want a helper class alongside your main class. That works fine:

public class MultipleClassesInSameFile {
    public static void main(String[] args) {
        IO.println(GenerateMessage.generateMessage());
        IO.println(AnotherMessage.generateAnotherMessage());
    }
}

class GenerateMessage {
    static String generateMessage() {
        return "Here is one message";
    }
}

class AnotherMessage {
    static String generateAnotherMessage() {
        return "Here is another message";
    }
}

$ java MultipleClassesInSameFile.java

Output:

Here is one message
Here is another message

The first class in the file must have the public static void main method. The other classes can be package-private.

Multi-File Programs (JDK 22+)

JDK 22 added JEP 458, which lets the java launcher work with programs spread across multiple files and directories.

Say you have this structure:

project/
├── Main.java
├── model/
│   └── Person.java
└── service/
    └── PersonService.java

Main.java:

import model.Person;
import service.PersonService;

public class Main {
    public static void main(String[] args) {
        PersonService service = new PersonService();
        Person person = service.createNewPerson();
        IO.println(person.printName() + " has been created!");
    }
}

As long as model/Person.java and service/PersonService.java exist in subdirectories matching their package structure, the launcher will compile and load everything:

$ java Main.java

You can structure your code properly without setting up a build system. That matters more than it sounds — the jump from single-file programs to multi-file projects used to require a build tool right away.

Using JDK Classes

Any class from the standard JDK libraries works out of the box. No classpath configuration needed:

import java.util.Scanner;
import java.util.regex.MatchResult;

public class ScannerExample {
    public static void main(String... args) {
        String wordsAndNumbers = """
            Longing rusted furnace
            daybreak 17 benign
            9 homecoming 1
            freight car
            """;
        try (Scanner scanner = new Scanner(wordsAndNumbers)) {
            scanner.findAll("benign").map(MatchResult::group).forEach(IO::println);
        }
    }
}

$ java ScannerExample.java

The Scanner and MatchResult classes come from java.util, which is part of the JDK, so no extra setup is required.

External Libraries Need the Classpath

If your code depends on a library that is not part of the JDK, you still need to add it to the classpath manually:

import org.apache.commons.lang3.RandomUtils;

public class ReferenceNonJDKClass {
    public static void main(String[] args) {
        IO.println(RandomUtils.nextInt());
    }
}

$ java -cp /path/to/commons-lang3-3.12.0.jar ReferenceNonJDKClass.java

The -cp flag tells the launcher where to find the external JAR. This is one limitation of the direct-launch approach — it does not resolve dependencies for you. For anything beyond simple programs, you will eventually want a build tool like Maven or Gradle.

Shebang Files: Java as a Script

On Unix-like systems (Linux, macOS), you can run a single-file Java program like a shell script. Add a shebang line as the first line of your file:

#!/path/to/your/bin/java --source 26

public class HelloJava {
    public static void main(String[] args) {
        IO.println("Hello " + args[0]);
    }
}

Two requirements:

  1. The file cannot have a .java extension (try something like hello)
  2. You need to make it executable: chmod +x hello

Then run it like any other script:

$ ./hello Maria

This is handy for small utility scripts. Instead of writing bash or Python, you can write a quick Java program and run it directly from the terminal.

Be careful though — if you forget to pass an argument when the program expects one, you will get an ArrayIndexOutOfBoundsException. There is no safety net here.

When to Use This

Direct source-file execution is great for:

  • Learning and experimentation. Trying out a new API or Java feature without creating a full project.
  • Quick scripts. Shebang files turn Java into a viable scripting language for small tasks.
  • Teaching. No build tool setup means beginners can focus on the language, not the toolchain.
  • Prototyping. Multi-file support (JDK 22+) lets you structure code properly while still skipping the build step.

It is not a replacement for a proper build system. Anything that needs external dependencies, tests, or packaging should use Maven or Gradle. But for quick, self-contained programs, the java launcher saves you real time.

Summary

  • The java launcher can compile and run .java files directly without a separate javac step.
  • The compiler runs in memory — no .class files are written to disk.
  • You can pass arguments, define multiple classes in one file, and import JDK classes freely.
  • JDK 22 added multi-file support, letting you structure code across directories.
  • External libraries still need the -cp classpath flag.
  • On Unix systems, shebang files let you run Java programs like shell scripts.
  • Use this for learning, scripting, and prototyping. Use a build tool for everything else.

Based on dev.java/learn — https://dev.java/learn/launch-simple-source-code-programs/