Architecture & DesignUsing JShell in Java 9 in NetBeans 9.0, Part 6

Using JShell in Java 9 in NetBeans 9.0, Part 6

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Shell, or JShell, is designed not to develop end-user applications but to run and evaluate code snippets. The code snippets in turn could be used to develop applications. Without JShell, a complete application must be developed and tested and debugged. JShell with NetBeans 9.0 was discussed in several earlier articles. These are:

In this continuation article, we shall run lambda code snippets in JShell. We shall also explain how code snippets that run successfully could be saved to a file and reloaded if needed. This article has the following sections:

Using Lambda Expressions

Lambda expressions are a short form for classes with only one method. Anonymous classes also simplify syntax for a class and are more concise than a full class definition, but even anonymous classes that define only one method are too much code. Lambda expression syntax, in comparison, is more concise and could replace several lines of code with just 1-2 lines. As a comparison between an anonymous class and lambda expression, consider an anonymous class that implements the Runnable interface, which has only one method. The following code snippet defines a variable of type Runnable for an anonymous class that implements the Runnable interface.

Runnable r1 = new Runnable(){
   @Override
   public void run(){
      System.out.println("Hello from Anonymous Class");
   }
};

Invoke the run() function of the anonymous class.

r1.run();

Run the code snippets in JShell and the Hello message in System.out.println is output, as shown in Figure 1.

Running an Anonymous Class Code Snippet
Figure 1: Running an Anonymous Class Code Snippet

Next, create a lambda expression that implements the Runnable interface and outputs a message as follows.

Runnable r2 = () -> System.out.println("Hello from Lambda
   Expression");

Invoke the run() method implemented by the lambda expression.

r2.run();

Run the code snippet for lambda expression in JShell to output a Hello message, as shown in Figure 2.

Running a Lambda Expression Code Snippet
Figure 2: Running a Lambda Expression Code Snippet

The example of lambda expression we discussed is for a single-method interface Runnable that already is defined in the API. But, lambda expressions may be used for user-created interfaces with a single method. As an example, define an interface Hello that defines a single method hello(String).

interface Hello{
   String hello(String name);
}

First, create an anonymous class that implements the Hello interface. Assign the anonymous class to a variable of type Hello.

Hello hello= new Hello(){
   public String hello(String name){
      return "Hello "+name;
   }
}

Invoke the hello(String) method of the anonymous class.

hello.hello("Deepak");

Run the code snippets in JShell and a user-created anonymous class gets created. Its function invoked with output is shown in Figure 3.

Running a Code Snippet for a user-created anonymous Class
Figure 3: Running a Code Snippet for a user-created anonymous Class

Next, use a lambda expression to implement the same interface Hello and assign the lambda expression to a variable of type Hello.

Hello hello = (name) -> {return "Hello "+name;}

Invoke the hello(String) method of the lambda expression variable with a name supplied as an argument.

hello.hello("Deepak");

The output from the lambda expression implementation is shown in Figure 4.

Running Lambda Expression Implementation for a user-created interface with a single method
Figure 4: Running Lambda Expression Implementation for a user-created interface with a single method

Saving and Loading Code Snippets

Code snippets that have been run in JShell in a given session may be saved to a file for later reuse. The /save command saves the code snippets. The /save command has three forms, as discussed in Table 1.

/save Command Syntax Description
/save <filename> Saves the code snippets that have run successfully in a given session, the current active code snippets, to the specified file.
/save -history <filename> Saves the history of all the code snippets and commands, whether valid and active or failed, in a given session to a specified file.
/save -start <filename> Saves the current startup script to the specified file.

Table 1: The /save Command

Next, we shall demonstrate saving code snippets with an example. To find which code snippets shall be saved with /save command list, the active code snippets with the /list command are shown in Figure 5.

Listing Active Code Snippets
Figure 5: Listing Active Code Snippets

Save the active code snippets to a file, hello.jsh, with the following command.

/save hello.jsh

The active code snippets get saved with no output generated from the command (see Figure 6). The file in which the code snippets are saved is created in the C:incubating-netbeans-java-9.0-binnetbeansbin directory, the same directory in which the NetBeans applications netbeans64.exe and netbeans.exe are located.

Saving Active Code Snippets with /save
Figure 6: Saving Active Code Snippets with /save

The hello.jsh is saved as plain-text as listed:

interface Hello{
   String hello(String name);
}
Hello hello= new Hello(){
   public String hello(String name){
      return "Hello "+name;
   }
};

Next, we shall demonstrate opening or loading code snippets from a saved file. Because the current session is already active and loading code snippets to it won’t be of much use, first reset the Java Shell with Reset Java Shell, as shown in Figure 7.

Reset Java Shell
Figure 7: Reset Java Shell

The Java Shell gets reset, as shown in Figure 8.

Java Shell Reset
Figure 8: Java Shell Reset

Run the /open command in JShell and its syntax is output indicating that a filename is required, as shown in Figure 9.

The /open command requires a filename
Figure 9: The /open command requires a filename

To open the hello.jsh file saved previously, run the command /open hello.jsh, as shown in Figure 10.

Running the /open hello.jsh Command
Figure 10: Running the /open hello.jsh Command

The hello.jsh file gets opened into the current session and all the code snippets in the file become active. The /open command does not generate any output message if it runs successfully (see Figure 11).

Running the /open Command to open hello.jsh
Figure 11: Running the /open Command to open hello.jsh

List the active code snippets with /list. The code snippets opened from the file get listed, as shown in Figure 12.

Active Code Snippets from opened File
Figure 12: Active Code Snippets from opened File

The active code snippets from a file may be used just as first run code snippets. As an example, run another code snippet to invoke the hello(String) function of the hello variable.

hello.hello("Deepak");

The output from the new code snippet is shown in Figure 13.

Output from Code Snippet
Figure 13: Output from Code Snippet

The new code snippet gets added to the list of active code snippets, as shown in the /list command output in Figure 14. Once opened, code snippets from a file are not distinguished from new code snippets run in the current session.

Listing Active Code Snippets
Figure 14: Listing Active Code Snippets

The active code snippets may be re-saved with /save. All the active code snippets from the opened file and the new get saved. The file extension does not have to be .jsh. It could be any file extension. As an example, save the active code snippets to hello.txt with the /save hello.txt command, as shown in Figure 15. The command does not generate any message if run successfully.

Saving Active Code Snippets to hello.txt
Figure 15: Saving Active Code Snippets to hello.txt

Reset the Java Shell by selecting Reset Java Shell (see Figure 16).

Reset Java Shell
Figure 16: Reset Java Shell

In the reset Java Shell, run the /open hello.txt command to open the saved file, as shown in Figure 17.

Opening hello.txt
Figure 17: Opening hello.txt

The code snippets from hello.txt get added to the current session. They are listed with /list command as active code snippets, as shown in Figure 18.

Listing Code Snippets opened from hello.txt
Figure 18: Listing Code Snippets opened from hello.txt

The file to which code snippets are saved could have any file extension supported by the filesystem, as an example hello.abc!jsh,txt. The file could have no extension at all. Code snippets get saved as plain-text regardless of their file extension. As an example, save code snippets in a file without an extension hello with following command.

/save hello

Reset the Java Shell with Reset Java Shell, as shown in Figure 19.

Saving Active Code Snippets to File hello
Figure 19: Saving Active Code Snippets to File hello

In the reset Java Shell, run the following commands to open and list code snippets from the hello file.

/open hello
/list

The code snippets opened from file hello get listed, as shown in Figure 20.

Code Snippets loaded from File hello
Figure 20: Code Snippets loaded from File hello

We discussed opening files that were previously saved with /save. But, user-created, plain-text files in the C:incubating-netbeans-java-9.0-binnetbeansbin directory also may be opened with the /open command.

Conclusion

Over several articles, we introduced using Java Shell (or JShell) with NetBeans 9.0. Support for Java Shell is a new feature in JDK 9 and NetBeans 9. JShell is designed to run and test code snippets. Code snippets don’t need the context of a class and may be run at the top level. Some modifiers that are supported in a file-based Java application. such as the access modifiers public, private, or protected, are not significant in JShell and all variables are accessible to the current session of the shell. Other modifiers, such as final, and static, are not permitted in top-level declarations, and are ignored. We discussed running code snippets for all kind of Java constructs including variables, classes, methods, enums, control flow statements, and lambda expressions. We also discussed saving code snippets to a file and opening code snippets from a file.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories