Chapter 20. JShell: the Java Shell
JShell, originally called Project Kulla, is an interactive command-line read-eval-print-loop (REPL) tool introduced in the Java 9 SDK. Similar in functionality to such interpreters as Python’s ipython
and Haskell’s ghci
, JShell allows users to evaluate and test fragments of code in real time without the trouble of creating a test project or a class housing a main function.
The code in this chapter was tested against JShell version 9-ea
.
Getting Started
JShell can be launched from the menu of the NetBeans IDE (Tools→Java Platform Shell), from the Windows command line by running jshell.exe
from the /bin/ directory of your JDK installation, or in POSIX environments with the jshell
command.
When the environment has loaded, you will be greeted with a prompt:
|
Welcome
to
JShell
--
Version
9
-
ea
|
For
an
introduction
type:
/
help
intro
jshell
>
From here, you will be able to enter, execute, or modify code snippets, or interact with the JShell environment through its built-in commands.
Snippets
JShell operates upon units called snippets, which are code fragments entered by the user at the jshell>
prompt. Each snippet must take a form defined in the JLS, as summarized in Table 20-1:
Java Language Specification Production | Example |
---|---|
Primary |
|
Statement |
|
ClassDeclaration |
|
MethodDeclaration |
|
FieldDeclaration ... |
Get Java Pocket Guide, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.