O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Our module will depend on the jdk.jshell module. So, the module definition will look like the following:
        module jshell{          requires jdk.jshell;        }
  1. Let's create an instance of the jdk.jshell.JShell class by using its create() method or the builder API in jdk.jshell.JShell.Builder:
        JShell myShell = JShell.create();
  1. Let's read the code snippet from System.in using java.util.Scanner:
        try(Scanner reader = new Scanner(System.in)){          while(true){            String snippet = reader.nextLine();            if ( "EXIT".equals(snippet)){              break;            }            //TODO: Code here for evaluating the snippet using JShell API          }        }
  1. We will use the jdk.jshell.JShell#eval(String snippet) method to evaluate the input. Evaluation will result in a list of jdk.jshell.SnippetEvent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required