JShell

After Python, Ruby, JavaScript and everyone else, Java will finally get its official Java Shell with Java 9. One of the main reason for the developers to finally introduce the Java Shell, is to lower the learning curve for initial learning. They state, that the primary reason why some schools move away from Java is the missing shell.
But the Java Shell could also be interesting for more experienced developers.

How does it work?

The Java Shell (JShell) is a REPL (Read-Eval-Print-Loop). That means, you are able to enter your code, press enter and see the result instantly. You do not need to go through the usual process where you edit the code, save the file, compile, launch and then see the result. Everything you enter will be interpreted as pure Java, except you start with a slash to enter a command.

You can enter /help to see a list of all commands, some of which are presented in the following examples. The Java Shell also provides a context for your code. So you do not have to enter “public class Clazz { public static void main…” if you only want to test a code-snippet. Additionally, you do not need semicolons, if you are not creating a method. You can see that here:

HelloWorld

We just enter “Hello World”. No “public class …”, no “String str = …”, no Semicolon at the end of the line.
Because of the quotation marks, the input is interpreted as a String and saved into a String-Variable named $1. Of course, you could write “String str = …”, but per default it is not required.

You can output this String in the next step, or call some methods on it. As you see, we replaced “Hello” with “Goodbye”, and the result (“Goodbye World”) is saved to $3.
Declaring methods is also possible with the JShell. These could be especially useful, if you want to do some calculations, without having to create an entire application for that.

Faculty

As you see, we declare a method, that calculates the faculty. Then we simply call it and see the result in the command line. Very handy, right? Of course, you can create classes, too. You can also edit the methods you created. So you might be able to create an application, that modifies it’s own source code.
Another use case for the JShell is for you to try out new APIs with it. Here is an example for a beginner, who just figured out, how Swing works:

Swing

It is possible for you to save your work to a file with /save . Then the file will contain all the lines you entered and works as a script. When you open the file with /open , the lines will be executed immediately.
Let’ see, how we can use this in the next example, which will be a HTTP-Client:

import java.net.http.*
import java.lang.Object

void getHello() throws Exception{
	HttpResponse resp = HttpRequest.create(new URI(
		"http://127.0.0.1:8080/hello")).GET().response();
	System.out.println("[" + resp.statusCode() + "] "
		+ resp.body(HttpResponse.asString()));
}

getHello()

This program will send a get request to a server and outputs the status code and body of the response.
This is, what happens, when we open the script with /open:

Http_Load

As you see, the get-request is sent and the response is printed to the console.
The method getHello() is also stored in the background, so we can call it again, after the script is done:

Http_Load_2

Working with the JShell feels a lot, like working with something like Lua or some other scripting language. But the difference is, that you have a real object-oriented language backing you. You can use the big API delivered with the JRE/JDK, but you can also use third party dependencies, or your own libraries.
We, for example, used the JShell a lot when testing and investigating the updated ProcessAPI, coming with Java 9. It makes creating your own snippets or testing how a new API responds a lot more streamlined.

Finally, the JShell will have an API-feature for foreign, (non-)Java applications. You will be able to execute Java Code from applications, that are not necessarily written in Java. You would also be able to create a java-application, that can modify its own code. These kind of application have an even higher complexity, then using reflections and should be funny to debug.

Short URL for this post: http://wp.me/s4nxik-jshell
This entry was posted in Did you know?, Java Basics and tagged , , . Bookmark the permalink.

One Response to JShell

  1. elvira says:

    Having an official shell for Java is cool. But it would be nice to have an official interactive development environment like Jupyter for Python. I hope that a Jupyter like environment or (better) official Java support for Jupyter is the next step.

Leave a Reply