Introduction to JShell : The Java Shell

In this article we will be taking a look at the new tool, JShell, which is a part of the Java 9 release.

What is JShell ?

It is an interactive tool or shell also referred to as REPL ( Read- Evaluate-Print loop) that is packaged as part of  Java 9 release. REPL is basically Reading input or data typed by the user, Evaluating the input , Printing the result that was evaluated and then going back to read the next input(Looping).

Why JShell ?

It is a very handy tool for developers both old and new wanting to explore the Java language by trying or experimenting snippets of code.

First time Java developers who wish to get started with the Java language can use JShell to write small pieces of code and later on move to IDE’s like Eclipse, STS, IntelliJ to set up full fledged projects.

For the experienced Java developer who wants to explore a new API or try out short  examples, the JShell can be quite useful. Let us say you are in the midst of fixing a bug or implementing a new functionality in an existing class using an API you have not tried before. Typical way to do this would be to create a new class with a main method in your code repository, type in the sample code for the API, right-click and run. Finally, we would copy the code and paste it somewhere in the existing code. As we age, we tend to forget a few things like deleting the sample file with the main method or sometimes we might even create a main method in an existing class where the new code is needed. While checking in, we might just check in the file with the main method only for your lead to do a code review which will obviously be rejected. This is assuming your lead does a proper code review !

Starting JShell

Open a command prompt, go to your  <Jdk 9  Installation>/bin directory and type jshell. You can also add it to your environment variables so that it can be started from any directory on the command prompt.

Some of the things we can do with JShell

  • Additionaddition

You don’t need JShell to check that 2+3=5. However make note of the variable created above , $1. This is a temporary variable. But, did you realize that we did not write a class with a main method with a System.out.println to print the result of 2+3 ?

  • Exploring Lambdas, the shiny new feature – Wait a minute, these are more like shiny old features introduced in Java 8!Consumer_Lambda

Did you notice the nice lambda created above ?

  • Can’t remember the method in the Consumer interface?Consumer_Lambda_Tab

Type in the name of the variable(s) created above , followed by a dot and press tab ! You get a bunch of methods that can be called on the Consumer interface. For the Consumer interface, it is the accept method that we are interested in.

  • Let’s try something with Streamsstreams

The moment you create a Stream which is referenced by the strings variable above, notice the type of strings. It is a ReferencePipeLine, JShell returns the type of the variable too!

  • Create a class, declare methods and variablesclass

Notice that after typing class Test { and pressing enter, JShell is intelligent enough to figure out that you are not yet done with the current statement and hence it waits for the next line of code.

  • Listing out variables declared so farvars

Get a list of all the variables using the /var command. To get a list of all the methods, use the /methods command.

  • Listing all the code typed in so farlist

Use the /list command. We performed an addition, created a Lambda , a Stream , created a class, then an object and called the getValue method. The /list command, simply lists them.

  • Typed in some code, time for a tea break, worried about machine crash ?save

You can save your code snippets using the /save command. Make sure you have the right permissions to save/create a file. You can follow that up with a /open command which will simply execute the code in the file.

  • Made a mistake, want to edit something ?editTyping the /edit command as shown above opens up a window where you can go and edit the code you typed. Using our knowledge of method reference, we can change the lambda expression from (String s) -> System.out.println(s) to System.out::println. On clicking the accept button, the change is reflected.

editpost_edit

Notice that the /vars command now lists the updated reference to the Lambda expression.

  • Create a package

Come on , JShell is not for that. The entire thing is just in a package. Now, don’t whine that JShell does not allow you to create packages. It is not meant to do that !

  • Want to quit JShell ?

Inform JShell by typing  /exit. JShell politely says GoodBye to you. Well, you could be a little rude and press Ctrl-D to exit JShell, in that case don’t expect JShell to say Goodbye!

Conclusion

JShell is a great addition to the JDK 9 release. Use it to experiment snippets of code and get immediate feedback. This article was just a quick introduction. JShell also has an API which can be used to integrate it into an IDE or a tool.

JDK 9 reached GA on 21st September 2017, to take a look at all the features, visit the openjdk site here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: