Embracing Java 9 ~ Working With JShell

Why jShell ?

Have you ever felt the pain of writing the simple ‘Hello World’ program in Java as a beginner? To print a simple one line onto the console, you have to go through the java jargons such as a public static void main method with proper string array arument, enclose it inside a class and finally write the one line that you wised to execute. Definitely not a pleasant exercise for a newbie especially who selects java as his first programming language. Java 9 puts an end to this misery by means of Java Shell or jShell.

What is jShell ?

Jshell is Java’s REPL implementation like other modern languages such as Scala, Ruby, Groovy etc. The Java Shell (jShell) is a Read-Eval-Print Loop implementation by Java to facilitate instant execution of commands, statements, expressions and even methods – without requiring to create any class or the good old main method. Sounds fun isn’t it? We will shortly do some hands on to learn more about jshell, but first let us have a look at the feature set of this REPL tool:

  • A command-line tool to¬†interactively declare and evaluate variables, statements, expressions, enums etc.
  • Write methods and execute instantly without any object instantiation; no ‘javac‘ to compile first, run later
  • Automatic addition terminal semicolons
  • Build-in ‘help’ to facilitate understanding of the tool
  • It has general purpose imports already inbuilt into it. You can customize or add additional imports as required.
  • API support for other applications to leverage jshell functionalities
  • Tab completion support to give hints to select
  • Launch external editor for convenience
  • List previously executed commands, declared variables, functions

Hands-on with jShell

A. Setting up jShell

To best understand a programming language what should we do ? Well, write programs ūüôā ¬†… so lets do that by installing JDK 1.9 and configuring the PATH variable and launching the jshell.

Here is what we are going to do:

1.  Download and install JDK 9 early access build from here and launch the executable to install it.

2.  SET PATH=%PATH%;C:\Program Files\Java\jdk-9\bin; [I am on windows]

3.  Either go to C:\Program Files\Java\jdk-9\bin directory and launch the jshell.exe Or execute jshell command in the command prompt

B. Programming in jShell

Run in verbose mode [jshell -v] to get some idea of what’s being done behind the scene when you execute commands in the jshell.

1. Good old ‘hello world’

As simple as that !

 

2. Few basics about jShell

Lets declare two variables of type int, assign some values and perform simple addtion. In the below snippet we will also learn about scratch variables, few utility commands such as /vars, /list, /methods, /types. These commands are distinguished from snippets by a leading slash

To see default imports of jShell, type in /imports and hit enter.

 

3. Working With Method

a. declare method
b. execute method
c. change method definition
d. list all methods

You may also like  Java8 Diamond Problem - Solving Java Way
 

4. Tab Completion

There is a code hint utility embedded into the jshell. Type any command and press TAB key to get code help. If multiple completion options are there it will be listed, otherwise it will be autocomplete .

Try it out: Type System.curr and press TAB

When you are at a method call’s open-parenthesis, tab will show completion options and will show the parameter types:

Pressing tab again will show a plain-text version of documentation of the first method.

Each tab will fetch next level of documentation in this manner.

5.  Snippet Transformation

While writing code snippets that require addition class or package import, press <shift-tab>i (hold down shift while pressing tab, then release and press i) to bring up a menu to choose and do automatic import:

Note: there may be more than one choice of import.

After entering an expression, you can convert the expression to a variable declaration with <shift-tab>v. That is, the expression will become the initializer of the variable declaration and type of the variable will be the type of the expression:

The cursor will be positioned at the location where the variable name should go. Type the variable name and press return.

To bring up jshell help for these shortcuts, type /help shortcuts and hit enter

JShell has start-up entries that are silently and automatically executed before JShell starts, so that you can get to work quickly. These aren’t listed unless you request to see them with /list -start¬†or /list -all

6.  JShell Edit Pad

For few lines of code, JShell inline editor is good enough. But when your code start to get bigger then you might want to use a text editor to modify your code in more convenient way.¬†JShell ships with an inbuilt utility just in case¬†‘JShell edit pad’. To launch edit pad, use /edit command with method name.¬†Change the method code in the editor and ‘Accept’¬†it to instruct JShell update the method definition. Jshell shows confirmation message in prompt. Once done, save it and then exit the window.

7. Edit Through External Editor

Edit pad should be good enough for most cases, however, if you are obsessed with some editor, there is still something for you to be delighted. You can configure JShell to edit using your favorite editor. Run /set editor command in JShell to configure the editor. I configured notepad++ as my default editor. To get back to jShell editor, type /set editor -default.

C. Navigation In JShell

JShell maintains a history of snippets and commands. Snippets may have multiple lines, up/down-arrow will navigate through individual lines of a snippet. Each successive up-arrow press would replace the current line with the most recent to next recent commands. You can move a snippet at a time by using control-up and control-down.

jshell> class C {
…> int x;
…> }
| created class C

up-arrow will fetch the last executed line – which is the closing } bracket.

Ctrl+up-arrow with fetch the last executed snippet as whole – which is the class C definition.

 

D. Searching in JShell

The inline search utility in JShell is a very useful and powerful feature. Press Ctrl-r followed by a string you wish to search for in your history.¬†The search will proceed backward, starting from the most recent entrees and include previous sessions of the jshell tool. With each char press the search tool refresh its search, you don’t have to press ‘Enter’ key:

jshell> /exit
| Goodbye

That’s all from this session of my Java9 series. I hope this will give some amount of insights about JShell. Try it out yourself, its fun and interesting. Put your questions and suggestions in the comment below. Feel free to share the knowledge with others.

Best Regards,
BanerjeeBabu

Leave a Reply

Your email address will not be published. Required fields are marked *