Have you ever needed to evaluate a Java class, or just perform some quick tests to see how a statement is working? Writing a full Java program just to test a single command might be a solution. A better one would be to use a REPL (Read Eval Print Loop) tool for an interactive test of Java constructs.
I’m going to write about the upcoming REPL tool of Java 9 soon. But before, I like to show how to perform this task with the recent Java 8 version.
The bad news: There is no REPL tool included.
The good news: Take Nashorn for your REPL experiments.
You may start Nashorn at the command line. Even better, you might run it inside the NetBeans IDE, using the terminal window .
To get in touch with Nashorn, we’ll invoke it, print a HelloWorld from Java and quit.
If no path links to jjs, we need to start it by typing the full path. You need to adopt the path to your environment.
var system = Java.type("java.lang.System")
Once the Java class is assigned to that var, you can invoke its methods.
system.out.println ("Hello from Java")
To finish Nashorn simply type
In my book Java Lambdas and (parallel) Streams , I show different tasks to query data by Lambda statements. In the next part of this short series, I’m going to show, how to create the example data and then interactively querying it.