☰ See All Chapters |
Java Shell Tool (JShell)
The feature that the java missed for years is a shell tool to execute the code from command line. To evaluate any declarations, statements, and expressions, we need to save it in java file and then compile to class file and then execute it from JVM. This process is no longer necessary with the introduction of Java Shell tool (JShell). The Java Shell tool is an interactive tool can be used to execute and test any declarations, statements, and expressions etc. easily. JShell is a Read-Evaluate-Print Loop (REPL) tool.
Uses of JShell
When you want to test single line statement, as an example System.out.println(20*5);, you have to go through all the below process:
Write a complete program, At least one class and one method should exist in program.
Compile it to class file and fix any compilation errors.
Run the program.
Evaluate the result.
Edit the program is result is wrong.
Repeat the above steps.
But using Jshell tool all the above steps are not necessary, open the tool and test the statements.
How to open JShell
Install JDK 9
Set path in environment variables. To know how to set path in environment variable refer our tutorial
Open command prompt
Type jshell and hit Enter
How to close JShell
To close JShell type /exit and hit Enter
In JShell Informative messages start with a vertical bar. If line starts from vertical bar, then that is a message.
Storing variables in JShell sessions
When a variable is declared in JShell, they will be stored in the current JShell session and can used at any time in the current session. In the below execution we store a String variable.
Stored variable will be shown in JShell using ==> characters. ==> separates the variable name and its value.
No we will access and print the stored variable.
When variable entered that doesn't have a name, JShell create assign variable to store the value. These variables start with $ sign. These variables are called as scratch variables.
Note:
While declaring variables using semicolon at the end of declaration is optional.
The declared variables will have global scope and can be used in any place.
Storing methods in JShell sessions
Like variables we can store methods in JShell sessions.
Continuation prompt (...>) is used when a multiline statements like above is entered.
Calling stored method is same as we call in real programs.
Testing Expressions using JShell
JShell can be used to test Java expression to get instant results.
Importing Packages to JShell
By default below packages are imported in JShell
import java.io.*
import java.math.*
import java.net.*
import java.nio.file.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.prefs.*
import java.util.regex.*
import java.util.stream.*
Available packages can be seen using /imports command
To import any unavailable package use import statement.
JShell Commands
To control the environment and display information within a session, JShell provides the below listed commands
Command | Description |
/! | re-run last snippet |
/-<n> | re-run n-th previous snippet |
/<id> | re-run snippet by id |
/? | get information about jshell |
/drop | delete a source entry referenced by name or id |
/edit | edit a source entry referenced by name or id |
/env | view or change the evaluation context |
/exit | exit jshell |
/help | get information about jshell |
/history | history of what you have typed |
/imports | list the imported items |
/list | list the source you have typed |
/methods | list the declared methods and their signatures |
/open | open a file as source input |
/reload | reset and replay relevant history -- current or previous (-restore) |
/reset | reset jshell |
/save | Save snippet source to a file. |
/set | set jshell configuration information |
/types | list the declared types |
/vars | list the declared variables and their values |
Tab Auto Completion in JShell
This is very useful feature of JShell. You can partially enter the name of the stored declarations, statements and press Tab.
If variable with the name message is stored in session then mess<Tab> will auto populate the variable message
If variable with the name message1 and message2 is stored in session then mess<Tab> will auto suggest the variables message1, message2. Autocomplete takes place only when Jshell finds only one stored element. If more than one element found then it auto suggests them.
All Chapters