Java 9 with JShell
上QQ阅读APP看书,第一时间看更新

Installing the required software on Windows, macOS, or Linux

We must download and install the latest version of JDK 9 (Java Development Kit 9) for our operating system from https://jdk9.java.net/download/. We must accept the license agreement for Java to download the software.

As happened with previous versions, JDK 9 is available on many different platforms, including but not limited to the following:

  • Windows 32-bit
  • Windows 64-bit
  • macOS 64-bit (formerly known as Mac OS X or simply OS X)
  • Linux 32-bit
  • Linux 64-bit
  • Linux on ARM 32-bit
  • Linux on ARM 64-bit

Once we have completed the installation for the appropriate version of JDK 9 based on our operating system, we can add the bin sub-folder of the folder in which JDK 9 has been installed to the PATH environment variable. This way, we would be able to launch the different utilities from any folder in which we are located.

Tip

If we don't add the bin sub-folder of the folder in which JDK 9 has been installed to the PATH environment variable in our operating system, we will always have to use the full path to the bin sub-folder when executing the commands. In the next instructions to launch the different Java command-line utilities, we will assume that we are located in this bin sub-folder or that the PATH environment variable includes it.

Once we have installed JDK 9, and added the bin folder to the PATH environment variable, we can run the following command in Windows Command Prompt or in macOS or Linux Terminal:

javac -version

The previous command will display the current version for the primary Java compiler included in the JDK that compiles Java source code into Java bytecodes. The version number should start with 9, as shown in the next sample output:

javac 9-ea

If the results of the previous command display a version number that doesn't start with 9, we must check whether the installation completed successfully. In addition, we have to make sure that the PATH environment variable doesn't include paths to previous versions of the JDK and that it includes the bin folder for the recently installed JDK 9.

Now, we are ready to launch JShell. Run the following command in Windows Command Prompt or in macOS or Linux Terminal:

jshell

The previous command will launch JShell, display a welcome message that includes the JDK version being used, and the prompt will change to jshell>. Whenever we see this prompt, it means we are still in JShell. The following screenshot shows JShell running in a Terminal window on macOS.

Tip

If we want to leave JShell at any time, we just need to press Ctrl + D in a Mac. Another option is to enter /exit and press Enter.

Understanding the benefits of working with a REPL

Java 9 introduced an interactive REPL command-line environment named JShell. This tool allows us to execute Java code snippets and get immediate results. We can easily write code and see the results of its execution without having to create a solution or project. We don't have to wait for the project to finish the build process to check the results of executing many lines of code. JShell, as any other REPL, facilitates exploratory programming, that is, we can easily and interactively try and debug different algorithms and structures.

Tip

If you have worked with other programming languages that provide a REPL or an interactive shell such as Python, Scala, Clojure, F#, Ruby, Smalltalk, and Swift among many others, you already know the benefits of working with a REPL.

For example, imagine that we have to interact with an IoT (Internet of Things) library that provides Java bindings. We have to write Java code to use the library to control a drone, also known as a UAV (Unmanned Aerial Vehicle). The drone is an IoT device that interacts with many sensors and actuators, including digital electronic speed controllers linked to engines, propellers, and servomotors.

We want to be able to write a few lines of code to retrieve data from sensors and control the actuators. We just need to make sure things work as explained in the documentation. We want to make sure that the values read from the altimeter change when we move the drone. JShell provides us with the appropriate tool to start interacting with the library in a few seconds. We just need to launch JShell, load the library, and start writing Java 9 code in the REPL. With previous Java versions, we would have needed to create a new project from scratch and write some boilerplate code before we could start writing the first lines of code that interacted with the library. JShell allows us to start working faster and reduces the need to create an entire skeleton to start running Java 9 code. JShell allows interactive exploration of APIs (Application Programming Interfaces) from a REPL.

We can enter any Java 9 definition in JShell. For example, we can declare methods, classes, and variables. We can also enter Java expressions, statements, or imports. Once we have entered the code to declare a method, we can enter a statement that uses the previously defined method and see the results of the execution.

JShell allows us to load source code from a file, and therefore, you will be able to load the source code samples included in this book and evaluate them in JShell. Whenever we have to work with source code, you will know the folder and the file from which you can load it. In addition, JShell allows us to execute JShell commands. We will learn about the most useful commands later, in this chapter.

JShell allows us to call the System.out.printf method to easily format output we want to print. We will take advantage of this method in our sample code.

Tip

JShell disables some features from Java 9 that aren't useful in the interactive REPL. Whenever we have to work with these features in JShell, we will make it clear that JShell will disable them and we will explain their effects.

The semicolon (;) is optional at the end of a statement in JShell. However, we will always use a semicolon at the end of each statement because we don't want to forget that we must use semicolons when we write real-life Java 9 code in projects and solutions. We will only omit the semicolon at the end of a statement when we enter expressions to be evaluated by JShell.

For example, the following two lines are equivalent and both of them will print "Object-Oriented Programming rocks with Java 9!" as a result of their execution in JShell. The first line doesn't include a semicolon (;) at the end of the statement and the second line includes the semicolon (;). We will always use the semicolon (;) as in the second line, to keep consistency.

System.out.printf("Object-Oriented Programming rocks with Java 9!\n")
System.out.printf("Object-Oriented Programming rocks with Java 9!\n");

The following screenshot shows the results of executing the two lines in JShell running on Windows 10:

In some examples, we will take advantage of the fact that JShell provides us networking access. This feature is extremely useful to interact with Web Services. However, you have to make sure that you don't have JShell blocked in your firewall configuration.

Tip

Unluckily, at the time I was writing this book, JShell didn't include syntax highlighting features. However, you will learn how to use our favorite editor to write and edit code that we can then execute in JShell.

Checking default imports and using auto-complete features

By default, JShell provides a set of common imports and we can use the import statement to import the necessary types from any additional package we might need to run our code snippets. We can enter the following command in JShell to list all the imports:

/imports

The following lines show the results of the previous command:

| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*

As happens when we write Java code outside of JShell, we don't need to import the types from the java.lang package because they are imported by default and they aren't listed when we run the /imports command in JShell. Thus, by default, JShell provides us access to all the types in the following packages:

  • java.lang
  • java.io
  • java.math
  • java.net
  • java.nio.file
  • java.util
  • java.util.concurrent
  • java.util.function
  • java.util.prefs
  • java.util.regex
  • java.util.stream

JShell provides auto-completion features. We just need to press the Tab key whenever we want to have assistance from the auto-complete feature, as done when we work with the Windows Command Prompt or the Terminal in macOS or Linux.

Sometimes, there are too many options that start with the first characters we entered. In these cases, JShell provides us with a list of all the available options to provide us help. For example, we can enter S and press the Tab key. JShell will list all the types imported from the previously listed packages that start with an S. The following screenshot shows the results in JShell:

We want to enter System. Considering the previous list, we will just enter Sys to make sure that System is the only option that starts with Sys. Basically, we are cheating to understand how auto-completion works in JShell. Enter Sys and press the Tab key. JShell will display System.

Now, enter a dot (.) followed by an o (you will have System.o) and press the Tab key. JShell will display System.out.

Next, enter a dot (.) and press the Tab key. JShell will display all the public methods declared in System.out. After the list, JShell will include System.out. again to allow us to continue entering our code. The following screenshot shows the results in JShell:

Enter printl and press the Tab key. JShell will complete to System.out.println(, that is, it will add an n and open parenthesis ((). This way, we just have to enter the arguments for the method because there was just one method that started with printl. Enter "Auto-complete is helpful in JShell"); and press Enter. The following line shows the complete statement:

System.out.println("Auto-complete is helpful in JShell");

The following screenshot shows the results in JShell after running the previous line: