skip to content

Single-File Source Execution

Since Java 11 you can run a .java file directly with the java command, compiling in memory with no javac step, which makes shebang-style Java scripting possible. Interviewers raise it alongside JShell as evidence Java has gotten lighter for small tasks.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is single-file source-code execution in Java, and how do you run a .java file without compiling it first?

level: juniorimportance: should knowfreq 55%

answer

  1. JEP 330, Java 11
  2. `java Hello.java` — compile in memory, no .class
  3. One source file only — no separate compilation
  4. Args after the filename go to main
  5. Shebang needs `--source <version>`

basics

~10 s

Since Java 11 you can run a single .java file directly with java MyApp.java. The launcher compiles it in memory and runs it immediately, so you don't need a separate javac step.

solid answer

~40 s

Single-file source-code execution, introduced by JEP 330 in Java 11, lets the `java` command run a source file directly: `java Hello.java`. Normally you'd `javac Hello.java` to produce a `Hello.class` file and then `java Hello`. With this feature the launcher detects you passed a `.java` file, compiles it in memory (no `.class` written to disk), and immediately runs its `main` method. Anything after the file name is passed as program arguments. It's aimed at small programs, learning, and scripting where the two-step compile-then-run cycle is overhead. It only handles one source file: code split across multiple `.java` files still needs ordinary compilation. It pairs with JShell as Java's answer to lightweight, script-like usage.

code

java · 8 lines
java
// File: Greeter.java  — run with:  java Greeter.java Alice
public class Greeter {
    public static void main(String[] args) {
        String who = args.length > 0 ? args[0] : "world";
        System.out.println("Hello, " + who + "!");
        // No javac step, no Greeter.class on disk — compiled in memory by the launcher.
    }
}

go deeper

for a junior

Knows you can do java Hello.java directly since Java 11 and that no separate javac step is needed for a quick one-file program.

for a middle

Explains in-memory compilation (no .class), that args after the filename reach main, and the single-file limitation.

for a senior

Articulates JEP 330's intent (lower the on-ramp / scripting), the shebang + --source mechanism, and positions it against JShell.

for a principal

Frames it within Java's broader 'reduce ceremony' arc (JShell, implicit classes/instance main, JEP 458 multi-file source launch) and the trade-offs of script-mode vs build tools for real projects.

## The problem it solves Traditionally, running even a one-line Java program takes **two steps**: 1. **Compile** the human-readable source (`Hello.java`) into bytecode with the *compiler*: `javac Hello.java`. This writes a `Hello.class` file to disk. *Bytecode* is the intermediate instruction format the Java Virtual Machine (the *JVM* — the program that actually runs Java) understands. 2. **Run** the bytecode with the *launcher*: `java Hello`. For a beginner or for a quick script, that ceremony is friction. **JEP 330** ("JEP" = JDK Enhancement Proposal, the design-document process for changes to Java) shipped in **Java 11** to remove it. ## What the feature does If you give the `java` launcher a file name that ends in `.java` (instead of a *class name*), it switches into **source-file mode**: ``` java Hello.java ``` Under the hood it: - Reads the source file. - Compiles it **in memory** using the built-in compiler — **no `.class` file is written to disk**. - Loads the resulting class and invokes its `main` method. So one command replaces the compile-then-run pair. The decision is based on **the file extension / form of the first argument**: if it ends in `.java`, source mode; otherwise it's treated as a class name (or a JAR via `-jar`). ## Arguments Everything **after** the source file name is handed to the program as its `args`: ``` java Hello.java alice bob ``` Here `alice` and `bob` become `args[0]` and `args[1]` in `public static void main(String[] args)`. ## Shebang / script-like usage Because it's a single command, you can make a Java file behave like a shell script on Unix-like systems by adding a *shebang* line (the `#!` first line that tells the OS which interpreter to run). You launch it with an explicit `--source` flag because the file has no `.java` extension: ``` #!/path/to/bin/java --source 21 public class Hello { public static void main(String[] a){ System.out.println("hi"); } } ``` Make it executable (`chmod +x hello`) and run `./hello`. The `--source <version>` flag tells the launcher which language level to compile against and is **required** when the file extension isn't `.java`. ## Limitations (important) - **Exactly one source file.** The feature compiles only the file you name. It cannot do *separate compilation* — i.e. compile several source files that reference each other. If your `Hello.java` references another class defined in `Util.java`, source mode won't compile `Util.java` for you; you'd need ordinary `javac`/`java` or a build tool. (You *can* define multiple classes **inside** the single file — only the first/declared public class with `main` is the entry point in older versions; later Java relaxed which class can be the entry.) - **No `.class` output**, so it's not how you build/ship an application. - It can still **use libraries** on the classpath via `--class-path`/`-cp`, but it won't compile your own multi-file project. ## Where it fits Java's lightweight-usage story has two complementary tools: - **JShell** (Java 9): an interactive REPL ("Read-Eval-Print Loop") where you type expressions and see results immediately — great for experimenting line by line. - **Single-file execution** (Java 11): run a *whole small program* in one shot, ideal for scripts and teaching. Later, **JEP 445/463/477/512** ("implicitly declared classes and instance main methods", finalized in Java 25) reduced the boilerplate even further so a beginner can write just a `main` body without an explicit `class` or `String[] args`, which combines naturally with single-file execution.

  • How do you pass program arguments when using single-file execution?
    Put them after the file name: `java Hello.java arg1 arg2`. Tokens after the `.java` file are the program's `args`, not JVM options.
  • Can a single-file program use a third-party library?
    Yes — add it to the classpath with `--class-path`/`-cp`. The feature only restricts compiling *your own* multiple source files; classpath dependencies are fine.

saying these in an interview costs you the question

  • Saying it writes a .class file to disk (it compiles in memory)
  • Claiming it can compile a multi-file project (single file only — no separate compilation)
  • Confusing it with JShell (REPL) — they are different tools
  • Thinking you still need to run javac first

context

open as a page

What are the limitations of single-file source-code execution, and when should you use a build tool instead?

level: middleimportance: should knowfreq 48%

basics

~20 s

It runs only one .java file — it can't compile several source files that depend on each other, and it writes no .class output. Use a real build tool (Maven/Gradle) once your project spans multiple files or needs dependencies and packaging.

open as a page

How does single-file source execution compare with JShell, and when would you choose each?

level: middleimportance: nice to knowfreq 35%

basics

~20 s

JShell is an interactive prompt (REPL) where you type and evaluate code line by line. Single-file execution runs a whole small program from a .java file in one command. Use JShell to explore; use single-file mode to run a script.

open as a page

How do you turn a Java source file into an executable Unix script using single-file execution, and why is the --source flag needed?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Add a shebang line #!/path/to/java --source 21 as the first line of an extension-less file, make it executable with chmod +x, and run ./myscript. The --source flag is required because, without a .java extension, the launcher needs to be told to use source mode and which language level.

open as a page

Where does single-file source execution sit in Java's evolution toward lower-ceremony programs, and how have JEP 458 and implicit main methods extended it?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Single-file execution (Java 11) was an early step to make Java easier for small programs. Later, JEP 458 let you run multi-file programs from source, and implicit classes plus instance main methods removed most boilerplate, so a beginner can run a tiny program from source with almost no ceremony.

open as a page