Contents
- What is REPL?
- Jshell (Java Shell Tool)
- Install Jshell
- Advantages of Jshell
- Using Jshell
What is REPL?
Jshell (Java Shell Tool)
Advantages of Jshell
- 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.
Installing Jshell
❯ jshell
| Welcome to JShell -- Version 16.0.1
| For an introduction type: /help intro
jshell>
❯ $JAVA_HOME/bin/jshell
| Welcome to JShell -- Version 16.0.1
| For an introduction type: /help intro
jshell>
Using Jshell
- Print Statements
- Using Variables
- Expressions
- Defining and invoking methods
- Forward References
- Exceptions
- Auto Completion with Tab for Snippets
Jshell Commands
- /import
- /vars
- /methods
- /lists
- /history
- Tab Completion for commands
- Search
jshell> <Ctrl+R>
((reverse-i-search)`':
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