Core Java

Java 9: JShell

JShell is a new tool introduced in Java 9 that evaluates Java statements entered on the command line. It is the first offical REPL (Read-Evaluate-Print Loop) implementation for the Java platform, and it is great for trying out Java code without having to fire up an IDE or write a full program!

To run JShell, simply type jshell on the command line. Obviously, make sure that you have installed JDK 9 and that your JAVA_HOME environment variable is set correctly. You will see a prompt like this:

$ jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell>

Type /help at the prompt to see a list of available commands. To exit, type /exit.

You can enter code “snippets” and JShell will output the results. For example:

jshell> System.out.println("Hello World")
Hello World

You can auto-complete statements and also look at documentation using the Tab key:

jshell> System.out.
append(        checkError()   close()        equals(        flush()        format(        getClass()     hashCode()     notify()
notifyAll()    print(         printf(        println(       toString()     wait(          write(

Here is a screencast GIF showing JShell in action, created using LICECap:

Published on Java Code Geeks with permission by Fahd Shariff, partner at our JCG program. See the original article here: Java 9: JShell

Opinions expressed by Java Code Geeks contributors are their own.

Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button