skip to content

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

level: middleimportance: nice to knowfreq 35%

answer

  1. JShell = interactive REPL, fragments, stateful (Java 9)
  2. Single-file = one-shot run of a whole program (Java 11)
  3. JShell: no main/class needed; single-file: real main + args
  4. Explore in JShell, then capture as a runnable file
  5. Neither replaces a build tool

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.

solid answer

~50 s

Both are part of Java's lightweight-usage story but solve different needs. **JShell** (Java 9) is a REPL — a Read-Eval-Print Loop — an interactive shell where you type snippets (expressions, statements, declarations) and immediately see results, with no `main` method or class boilerplate. It's ideal for exploring an API, testing a regex, or teaching concepts interactively. **Single-file source execution** (JEP 330, Java 11) is non-interactive: you write a complete program in one `.java` file and run it with `java File.java`, getting a normal program execution (with `main`, args, exit code). Choose JShell when you're experimenting incrementally and want instant feedback on fragments; choose single-file mode when you have an actual small program or script to run end-to-end, including in a shebang. They complement each other — explore in JShell, then capture the result as a runnable single file.

go deeper

for a junior

Knows JShell is an interactive prompt and single-file mode runs a whole file, and won't confuse the two.

for a middle

Explains REPL vs one-shot, the boilerplate differences (no main in JShell), and picks the right tool per task.

for a senior

Frames both as ceremony-reducing on-ramps, describes a prototype-in-JShell-then-capture-to-file workflow, and notes the boundary with build tools.

for a principal

Positions both within Java's evolving beginner/scripting story (implicit classes, instance main, multi-file launch) and reasons about teaching/onboarding and tooling-strategy implications.

## Two tools, one goal: lower Java's ceremony Before Java 9–11, even trivial Java required a class, a `main` method, a `javac` compile, and a `java` run. Two features chip away at that: ### JShell — the REPL (Java 9) **REPL** stands for **Read-Eval-Print Loop**: a program reads what you type, evaluates it, prints the result, and loops. Languages like Python and Ruby have had these for years; JShell brought one to Java. You start it by running `jshell` and then type code **fragments** — you don't need a `class` or a `main`: ``` jshell> int x = 21 * 2 x ==> 42 jshell> System.out.println("hi") hi ``` Each line is compiled and run immediately, and earlier declarations stay in scope. It's **interactive and stateful** within the session. Great for: learning, probing an API, quick math, validating a snippet. It is **not** how you ship or run a finished program. ### Single-file source execution (JEP 330, Java 11) This is **non-interactive**. You write a *complete* program in one file: ```java // run.java public class run { public static void main(String[] a){ System.out.println("done"); } } ``` and run the whole thing once: `java run.java`. It compiles in memory and executes `main`, with real program semantics — command-line `args`, an exit code, normal `System.out`/`System.err`. Great for: scripts, automation, reproductions, and anything you'd otherwise write as a shell script but prefer in Java (with a shebang + `--source`). ## Side-by-side | Aspect | JShell | Single-file execution | |---|---|---| | Interaction | Interactive REPL | One-shot run | | Input | Code fragments, no boilerplate | A complete `.java` file | | `main` needed? | No | Yes (or implicit `main` in modern Java) | | State across inputs | Persistent within a session | None (single program run) | | Command-line args | Not the model | Passed to `main` | | Typical use | Explore / learn / test snippets | Run a small program or script | | Introduced | Java 9 | Java 11 (JEP 330) | ## How they work together A natural workflow: **prototype** an idea interactively in JShell, then once it's right, **paste it into a single `.java` file** and run it (or schedule it via a shebang script). Both reduce friction; neither replaces a build tool for multi-file, dependency-heavy, packaged applications. ## Common confusion to avoid People often say "`java Hello.java` opens a REPL" — it does **not**. That runs the program once. The REPL is the separate `jshell` command.

  • Do you need to write a main method in JShell?
    No. JShell evaluates fragments directly, so you type expressions/statements without a class or main. Single-file execution, by contrast, runs a program and needs a main (explicit, or the modern implicit instance main).
  • Which would you pick to quickly test how a date-formatting API behaves?
    JShell — its instant, stateful, line-by-line feedback is ideal for probing an API. You'd graduate to a single file only when you have a whole program to run.

saying these in an interview costs you the question

  • Saying `java X.java` opens an interactive prompt (it runs once; that's not JShell)
  • Claiming JShell can run command-line args into a main (it's fragment-based, not program-based)
  • Treating them as the same feature or interchangeable
  • Thinking either is a substitute for Maven/Gradle on real projects

context