rgoussu@goussu: ~/library/java/jdk-and-tools
~/library/java/jdk-and-tools cat jshell.md

jshell — the Java REPL

# The jshell REPL — exploratory snippets, /commands, feedback modes, and scripting use.

Conceptsaved 2026-08-09 #java#jdk#tooling#repl

Overview

jshell (Java 9, JEP 222) is the JDK's read–eval–print loop. It accepts snippets — expressions, statements, method and class declarations — without a surrounding class or main, forward-references unresolved names, and silently swallows checked exceptions and semicolons on expressions. It is the fastest way to answer "what does this API actually return?" questions.

Key points

  • Snippets: declarations can be redefined freely; $1, $2… hold expression results; tab completion and Shift-Tab i auto-import as you type.
  • /commands: /vars, /methods, /types, /list show state; /edit opens an editor; /save and /open file.jsh persist and replay sessions; /env -class-path lib.jar adds libraries; /reset starts clean.
  • Feedback modes: /set feedback verbose|normal|concise|silent controls how chatty results are — verbose is instructive when learning, concise for quick work.
  • Scripting: jshell script.jsh or jshell -q - <<'EOF' … EOF runs non-interactive; --execution local runs snippets in-process (faster, shares the JVM), the default is a separate remote agent process.
  • With dependencies: launch as jshell --class-path "$(mvn dependency:build-classpath …)" or via build-tool integrations to explore a project's own classes.
  • When it beats a test: probing an unfamiliar API's behaviour, checking formatting or date-arithmetic edge cases, demonstrating something to a colleague. The moment the answer is worth keeping, move it into a real test — jshell sessions are disposable by design.

Examples

jshell> var xs = java.util.stream.IntStream.rangeClosed(1,5).boxed().toList()
xs ==> [1, 2, 3, 4, 5]
jshell> /set feedback concise
jshell> /save exploration.jsh

Related