REPL full form stands for read-evaluate-print-loop. REPL is an interactive shell which performs the read-evaluate-print-loop operations that means it reads the line, evaluates it and prints the output.
Jshell (Java Shell Tool)
Jshell or Java shell lets us execute the java code from the shell to perform REPL Operations. With Jshell, no need of all the boiler plate code to test snippets of code and evaluate results. Just write the code interactively and execute them without delay.
Advantages of Jshell
The usual java program development process involves following steps
Open editor and write the program
Save the file, compile and fix errors if any.
Run the program, check for runtime errors and fix them.
Repeat the process.
If you have added any new functionality or used a new library, it would take more time for testing. Jshell saves the time and boosts productivity by helping us to try out the code and explore the options as you develop the program. We can evaluate the individual statements, methods, different APIs from different libraries while we develop our program. More importantly, it doesn't replace IDE but as an extra tool to try out the code.
Installing Jshell
There is no separate installation process for Jshell utility. It comes as part of JDK 9 and above. To install required version of JDK, refer oracle download page and oracle archives page.
To start Jshell, enter jshell on the command line.
❯jshell
|Welcome to JShell -- Version 16.0.1
|For an introduction type: /help intro
jshell>
JDK 9 or greater versions must be installed on your system. If multiple versions of JDK exist in your system, set the JAVA_HOME path to JDK 9 or greater version to use the jshell or invoke them directly by specifying the fullpath.
❯$JAVA_HOME/bin/jshell
|Welcome to JShell -- Version 16.0.1
|For an introduction type: /help intro
jshell>
Using Jshell
Print Statements
Using Variables
We can declare variables and use them anywhere in the entire jshell session. Note that, semicolon at the end is optional.
Expressions
When an expression is entered that doesn't have a named variable, a scratch variable is created so that the value can be referenced later. A scratch variable start with $ symbol.
We can evaluate compound expressions as well line below.
Defining and invoking methods
We can define the methods as well and invoke them as shown below.
The below example shows the continuation prompt (...>) that is used when a snippet requires more than one line of input to complete and also we can change the definition of any variable, method, class anytime.
Forward References
JShell accepts method definitions that reference methods, variables, or classes that are not yet defined. But invocation can be done, only when all the references are defined.
Exceptions
Auto Completion with Tab for Snippets
When you enter snippets, use the Tab key to automatically complete the item. If the item can’t be determined from what was entered, then possible options are provided.
Jshell Commands
Jshell provides some meta commands which are not related to evaluating the snippets. These commands start with forward-slash(/).
/import
By default, 10 packages are imported and can also be imported any package by using import statement.
/vars
It is used to show all the variables.
/methods
It is used to show all the methods.
/lists
It is used to show the source code written so far.
/history
It shows snippets executed so far. A history of snippets and commands is maintained across JShell sessions. This history provides you with access to items that you entered in the current and previous sessions.
Tab Completion for commands
Similar to snippet completion, when you enter commands and command options, use the Tab key to automatically complete the command or option. If the completion can’t be determined from what was entered, then possible choices are provided.
Search
Search the history of Jshell snippets executed is a unique feature of JShell.
jshell> <Ctrl+R>
((reverse-i-search)`':
Searching & Macros Shortcuts
Ctrl+R
Searches backward through history
Ctrl+S
Searches forwards through history
Ctrl+X (
Starts a macro definition
Ctrl+X )
Finishes a macro definition
Ctrl+X e
Executes a macro
External Editor
If we have declared a lengthy function or class inside Jshell, we can edit it in the external editor with /edit command which is alternative to command prompt.
jshell> /edit printMyWebsite
jshell>
Scripts
A Jshell script is a sequence of al code snippets and Jshell commands in a file. We generally use scripts to set up your JShell session with import statements and code that you want available during the session.
jshell> /save mysnippet.jsh
jshell> /exit
| Goodbye
❯ ls mysnippet.jsh
mysnippet.jsh
Exiting Jshell
/exit command is used to exit Jshell.
jshell> /exit
| Goodbye
Jshell is a valuable addition to Java toolbox. Start using in your day to day development work and comment your experience.
Post a Comment