Technical Article => Programming => Java

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.