What is JShell?
JShell tool is made available in Java version 9 release. It is an interactive command line REPL (Read-Evaluate-Print-Loop) tool, not a new concept if you have worked with other programming languages, Erlang, Python, etc. JShell is used for trying out java functionalities without the hassle of creating an application with classes, etc.
Prerequisite
- JDK 9 or later version needs to be installed on your machine
- JDK file path needs to be added to your JAVA _HOME or PATH environment variables
- You can access JShell from the command line by typing jshell
Basics
Declaring variables
Declaring Methods
Declaring Classes
It is okay for small things, but I like working in an editor. Can you use an editor?
Yes, JShell has it’s own builtin editor.
Java’s editor is okay but I prefer using my favorite editor instead. Can you?
Yes, you can by setting /set editor “/editor/filepath” Then by typing /edit. Your fav. editor will open.
To reset back to the default /set editor -default
Useful Commands
/help – Used to display the documentation for JShell.
/var – Used to display list of declared variables for the JShell session.
/methods – Used to display a list of declared methods for the JShell session.
/types – Used to display a list of declared classes for the JShell session.
General Q &A
How to open JShell?
Type jshell in command line Interface.
How to exit JShell?
Does it have code Completion support?
Press Tab key to auto complete or find options
You can also read the method documentation by pressing the Tab key 3 times.
Does JShell have error handling support?
Does JShell automatically save a variables?
Yes, JShell automatically stores result of variables in what is called scratch variables.
I’ve written code in a file, can I run it in JShell?
Yes, By using /open <filepath> command.
Final Thoughts
A great tool for trying out new java functionality, and receiving immediate feedback. There are a few more functionalties, all of them can be seen via the /help command. Is this something you could think of using or are using?
Pingback: Java 10 - var - Spaghetti Code Jungle
Pingback: Java Versions & Features - Spaghetti Code Jungle
Pingback: Java 18 - Simple http Server - Spaghetti Code Jungle