Week 142 — What is jshell?
Question of the Week #142
What is jshell?
12 Replies
The JDK (Java Development Kit) provides a tool named
jshell
that can be used to run Java code in a REPL environment. This means that one can run the jshell
command and enter one or multiple lines of Java code which will get executed without requiring to put that code in a class or method.
For example, when running jshell
and typing System.out.println("Hello World");
, it will print Hello World
:
When running an expression in jshell, it will be automatically assigned to a variable consisting of $
and an increasing number:
Classes and methods can be executed in jshell
by typing the code for these classes and methods and then using them as one would use them in any other code. It is also possible to redefine methods by typing them again with a different body:
jshell
comes with the ability to automatically import classes (or other types). To use that, navigate to the class and press Shift
+Tab
followed by the i
key. This shows a prompt asking what to import. By typing the number, it will automatically import the type.
In addition to that, jshell
has many capabilities similar to other shells like bash
. For example, one can use the Up
- and Down
arrow keys to navigate between executed statements to execute them again or use the Left
and Right
arrow keys to edit something within a line before executing it.
It also provides many shortcuts that can also be found in shells like bash
. For example, Ctrl
+R
can be used to start a backwards i-search allowing to type something and jshell
will find the last entered line/statement containing that text.
When pressing Ctrl
+R
again, it will continue searching until it finds another executed statement/line containing that text:
Pressing Enter
will execute it like any other text.
Similar bash
shortcuts like pressing Tab
for automatic completion, Ctrl
+A
to go to the start of the line and Ctrl
+E
to go the end of a line are supported as well.
It is also possible to use some commands starting with /
within jshell
. For example, typing /help
will display a list of commands and /imports
shows the current imports.
jshell
comes with multiple startup scripts that simplify its use. When only running jshell
, it imports the classes in the packages listed above. Other scripts can be added by running jshell
followed by the name of the script. For example, jshell PRINTING
runs jshell
with the PRINTING
script that provides utility methods to print text to the console without needing to type System.out.println
:
The JAVASE
script automatically imports all public classes in exported classes of the Java SE modules. This can be useful to just use classes without needing to import them explicitly.
The TOOLING
script provides methods one can use to execute some common tools provided by the JDK directly from jshell
.📖 Sample answer from dan1st
JShell, or the Java Shell, is an interactive command-line tool introduced in Java 9. It functions as a Read-Evaluate-Print Loop (REPL) environment for the Java programming language.
Key features and uses of JShell:
Interactive Code Execution:
JShell allows developers to enter Java code snippets (declarations, statements, expressions) directly into the command line and receive immediate feedback on their execution.
Rapid Prototyping and Experimentation:
It is ideal for quickly testing out small pieces of code, exploring new API features, or trying different variations of methods without the overhead of creating a full Java class or project.
Learning and Exploration:
JShell provides a hands-on way for beginners to learn Java fundamentals and for experienced developers to experiment with unfamiliar APIs or language features.
Reduced Boilerplate:
Unlike traditional Java development, JShell eliminates the need for class definitions, main methods, and compilation steps for simple code snippets, making it more efficient for quick tests.
Dynamic Evaluation:
It evaluates code snippets as they are entered, displaying results directly in the console, facilitating a dynamic and iterative development process.
In essence, JShell provides a lightweight and interactive environment for working with Java code, making it a valuable tool for both learning and rapid development.
Submission from kirannarayan
It is a Interactive command line tool, It is used as a Read Eval Print Loop
Submission from kasinathan2008
A interactive shell in the terminal, where you can code in java. Just like with the python shell.
Submission from marcl4000
It's a Java REPL (Read, Evaluate, Print, Loop). It is utilized for interacting with java code directly, as if it were commands on a command line.
Submission from jessesousa
jshell is a java shell, is a interactive command line tool in java, It is very help for beginners to learn and experiment new concepts in java and can practice small code snippets with out main method and class definitions, which is more efficient and quick tests and displays results directly inthe console
Submission from soft.nithin
jshell is a program that responds to your statements without writing full
java program.
Submission from gaucheyy
JShell allows you to write and execute Java code interactively, line by line in you command line. You can also run entire scripts to test or prototype functionality quickly. It works similarly to the python command, providing a REPL (Read-Eval-Print Loop) environment for Java.
Submission from jazdazkurwami911
jshell
is a new "Read-Evaluate-Print-Loop" (REPL) tool introduced with Java 9. It performs a similar function to REPLs in other languages, such as the interactive modes of python
or node
-- allow a user to explore the language and experiment with libraries. It provides a much quicker feedback loop than "traditional" Java, which would require saving code in one or more source files, compiling it, and then running it.
Here is an example jshell
session:
You can see from the above that semicolons are optional. JShell adds them to snippets automatically, when possible.
It also supports expression completion using the Tab key, which was used to complete the DateTimeFormatter.ISO_LOCAL_DATE_TIME
constant.
You can also see that simply entering an expression caused the string representation to be printed (the line starting with $3
). This expression was not assigned to a variable, nor explicitly written to stdout with a println
. But JShell writes the result of expressions to stdout automatically, which provides quicker feedback to the user.
That said, nearly everything that can be done in a Java program can be done in JShell, such as importing types, defining new classes and interfaces, and assigning to variables:
Just as with other expressions, simply entering a variable name causes its string representation to be printedJShell has a number of "slash commands", which control the shell itself. Within
jshell
, type /<tab>
to get a list of the commands. Hit Tab a second time to see a summary of each command. The most import are probably:
- /help
or /?
- Shows JShell help
- /exit
- Quits the JShell session⭐ Submission from dangerously_casual
Submission from hmemmy