Jshell
The JShell also known as Java Shell Tool is an interactive REPL (Read-Evaluate-Print Loop) tool. JShell has been available since Java 9. It is an interactive tool where you can create, evaluate and run expressions instantly as they are entered. This is useful for beginners to quickly grasp Java programming language features, also for developers to quickly run expressions.
I have listed down below few useful Jshell commands as well as the response captured during that run. Using JShell you can create variable, methods as well as classes. Using JShell you can define and execute code snippets line after line, see the results and also can modify/update the already given commands.
For instance, after declaring a variable as int a = 10 creates an int variable a with value 10 assigned to it. This variable can be updated by just typing a = 20. In similar fashion, you can quickly define and perform operations on variables, methods and classes.
Let's start with intro command. This gives introduction to JShell tool.
1. /help intro
2. Declaration
You can declare a variable just by giving the datatype, variable name and value. Note that for one line code snippets you don't need to provide semicolon. As soon as you create a variable you will receive a response from JShell with an acknowledgment.
2.1. Variables
2.2. Methods Declaration
2.3. Class Declaration
2.3.1. Replace Class
As we have create a class clazz
above, we can create a class with same name again. This action would replace the earlier created class with the new one.
2.3.4. Instantiate Class and call method.
Once a class has been created, you can instantiate it and create an object of it. JShell acknowledges the same with Object's class name + hashcode.
3. List of commands
To see all the commands that you have run so far in JShell tool can be listed using /list
command.
/list
4. Semicolon can be ignored for single line snippets, but not when multiple line are part of the snippet.
5. Saving snippets
The code you have written can be save to a java file as below.
/save TestJava9.java
6. Delete snippets.
Using /reset
we can delete all commands/snippets. The commands ran so far will be deleted.
To delete, a specific snippet make use of the snippet number available from the sequence of execution. The same can be identified using /list command.
/drop identifier
Ex: /drop 1
7. Edit
Use /list
to see prior commands and use the number respective snippets.
The following dialog is the JShell default editor. As soon as I'm trying to edit any commands I have previously given, the command will be shown in the edit pad in editable mode. You can change the value as intended and click Accept. The changes will be reflected immediately.
Changed x value from 10 to 20.
8. Few commands are loaded when jshell is started. The same can be viewed using /list -start
9. List all preloaded snippets and the code snippets we executed.
/list -all
10. Execute specific snippet/command with respective number.
You can also execute recent command using /-1
, and last but one would be /-2
and so on. And nth command can be executed using /n.
However, once last one is re-execute with /-1
the order gets updated and the last but one is identified now using /-3
.
Why?
When you re-execute the nth command that would become the recently executed command and the earlier n-1th command now is at n-2th position in the sequence. The same can be viewed from below figure.
The following clearly demonstrates the same.
11. Listing Specific Type of Snippets
Listing all snippets is cumbersome, once you reach the extent where tracking becomes complex. This can be avoided by selecting specific type of commands.
For instance, we can list only variable declarations using /vars
classes use /types
.
And methods, use /methods
12. Change default editor
You can change default jshell GUI editor to your preferred choice. This is specially useful when you remotely accessing jshell through ssh and opening separate window is not an option.
Changing the editor back to the default JShell editor.
13. Startup file
You can specify a startup file for jshell such that when jshell is started, the given startup file will be used instead of the default one which preloads default snippets. Please be warned that the default snippets will not be loaded when custom startup file is used.
Startup file has extension .startup . Create a file with extension .startup for instance jshelltest.startup and set the same as startup file using command
/set start
jshelltest.startup.
15. Exiting jshell
/exit
is the command used to exit and terminate the jshell.