JShell -- The command line tool to run Java code in Java 9

  Pi Ke        2016-04-01 21:46:48       24,973        8    

Java 9 is currently a work-in-progress and is planned to be GAed in March 2017. Quite a few new features will be introduced in the new release. The coolest feature is project Jigsaw which is to modularize the Java packages so that a customized JDK can be built and shipped with only the necessary modules to fulfill their project requirement. Apart from this feature, another big new feature is project Kulla -- JShell.

In simple, JShell is a command line tool which can be used to run Java code snippet without wrapping them into classes. It is similar to tools for other languages such as Python, PHP etc. You can evaluate Java declarations, expressions, statements etc using JShell to test your understanding of Java language. It is quite useful while teaching Java in classes.

Since Java 9 will be released in a few months, we can only get early access version of Java 9 to try this new feature. After installing the early access version of Java 9. when type java -version at command line, you will see below version info :

java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+111)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+111, mixed mode)

JShell is located at [JDK_HOME]/bin and you can type jshell to launch the command line tool.

Basic functionality

After launching jshell, you can type Java statements which conform to Java Language Specification. For example, you can print "Hello JShell" with below statement :

Quite familiar, right? Just like other shell scripting tools. No need to run the standard public static void main(String[] args){} anymore.

With JShell, you can also evaluate an expression right away. For example, if you want to know what 2 + 4 is , you just need to type 2 + 4 and you will get the expression result.

Note, there is no semicolon after typing 2 + 4, that is not necessary when it's a bare statement. The tool automatically adds semicolon for you.

Edit history

JShell can also remember edit history, i.e.within a session, a variable declared and initialized in previous command can be used in another command. See below ;

Tab auto-completion

JShell also supports tab auto-completion which means when you type part of a data type or class name, the tool will try to auto complete your type. For example, when you type Str + [Tab], it will display a set of classes which match this string.

Forward reference

JShell supports forward reference which means you can declare method which calls other methods which are not declared yet. To run this method, you have to declare other methods later. Otherwise, you will get some error. After declaring other methods, you can then run the method you declared earlier.

Supported commands

JShell comes with a list of built-in commands to help work with the tool. You can show the list of commands with command /help.

-> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|
|     /list [all|start|]                             -- list the source you have typed
|     /edit                                          -- edit a source entry referenced by name or id
|     /drop                                          -- delete a source entry referenced by name or id
|     /save [all|history|start]                            -- Save snippet source to a file.
|     /open                                                -- open a file as source input
|     /vars                                                      -- list the declared variables and their values
|     /methods                                                   -- list the declared methods and their signatures
|     /classes                                                   -- list the declared classes
|     /imports                                                   -- list the imported items
|     /exit                                                      -- exit jshell
|     /reset                                                     -- reset jshell
|     /reload [restore] [quiet]                                  -- reset and replay relevant history -- current or previous (restore)
|     /classpath                                           -- add a path to the classpath
|     /history                                                   -- history of what you have typed
|     /help [|]                                -- get information about jshell
|     /set editor|start|feedback|newmode|prompt|format|field ... -- set jshell configuration information
|     /?                                                         -- get information about jshell
|     /!                                                         -- re-run last snippet
|     /                                                      -- re-run snippet by id
|     /-                                                      -- re-run n-th previous snippet
|
|  For more information type '/help' followed by the name of command or a subject.
|  For example '/help /list' or '/help intro'.  Subjects:
|
|     intro     -- An introduction to the jshell tool
|     shortcuts -- Describe shortcuts

These commands are quite self-explanatory. Here we just take the command /edit as an example,  this command can be used to update a previous typed statement. First, type /list to show all ids available. Then type /edit 6 to edit the statement String helloMessage = "Hello JShell";

A Swing dialog will show and allows you to edit this statement and save it. 

JShell is still a work-in-progress project and it will be more appealing when it's released finally. But it's really a cool tool which you may use frequently in the future.

JAVA 9  JSHELL  KULLA 

       

  RELATED


  8 COMMENTS


Anonymous [Reply]@ 2016-04-03 11:01:37

Nice article! Is this already available for linux/osx?

Ke Pi [Reply]@ 2016-04-04 01:25:22

Yes, The early access version now contains this command.

B. Singh [Reply]@ 2016-04-25 12:31:13

Good and short info on JShell @Ke Pi.

Do you know how to clear JShell Console? I tried through program but it doesn't work. 

Ke Pi [Reply]@ 2016-04-26 07:46:15

What do you mean by "clear"?

B. Singh [Reply]@ 2016-04-26 10:03:27

When we work on windows/Unix console/terminal we use cls/clear command to clear/clean console/terminal. In same way when you arw working on JSHELL then how to clear it. I hope you will understand what I am trying to ask this time.

Note: I think it is not there yet in EA of JShell..

Ke Pi [Reply]@ 2016-04-27 02:10:29

It's not supported yet. Not sure whether it will support in the official release. Will get to know when it's released next year.

Anonymous [Reply]@ 2016-06-13 10:50:15

I've used cmd+k on OS X to clear the Jshell. I don't know the exact hotkeys for linux, but the same may work.

Marcus [Reply]@ 2017-03-31 16:37:09

JShell changes the way you learn new things related to Java. In fact, there is a new book that teaches Java 9 with JShell



  RANDOM FUN

Which is the best programming language?