Tag Archives: repl

JDK 9 REPL: Getting Started (Tech Tip #87)

Conferences are a great place to meet Java luminaries. Devoxx France was one such opportunity to meet Java language architect, ex-colleague and an old friend – Brian Goetz (@briangoetz). We talked about JDK 9 and he was all raving about REPL. He mentioned that even though there are a lot of significant features, such as modularity and HTTP2 client, in Java SE 9, but this tool is going to be talked about the most often. The statement makes sense since it will really simplify exploration of Java APIs, prototyping, demos in conferences, and similar tasks a lot simpler. This blog is coming out of our discussion there and his strong vote on REPL!

Read-Evaluate-Print-Loop has been there in Lisp, Python, Ruby, Groovy, Clojure, and other languages for a while. Unix shell is a REPL which can read shell commands, evaluate them, print the output, and goes back in the loop to do the same thing.

JDK 9 REPL

You can read all about REPL in JDK 9 in JEP 222. Summary from the JEP is:

Provide an interactive tool which evaluates declarations, statements, and expressions of the Java Programming Language: that is, provide a Read-Evaluate-Print Loop (REPL) for the Java Programming Language. Also, provide an API on which the tool is built, enabling external tools to supply this functionality.

JEP 222

The motivation is also clearly spelt out in the JEP:

Without the ceremony of class Foo { public static void main(String[] args) { … } }, learning and exploration is streamlined.

JEP 222

JEP 222 targets to ship REPL with JDK 9 but openjdk.java.net/projects/jdk9 does not list it is as “targeted” or “proposed to target”. Seems like a documentation bug ðŸ˜‰

 

As of JDK 9 build 61, REPL is not integrated, and needs to be built separately. Eventually, at some time before JDK 9 is released, this tool will be integrated in the build.

Lets see what does it require to have it running on OSX. This blog followed Java 9 REPL – Getting Started Guide to build and run REPL. In addition, it provides complete log output from the commands which might be helpful for some.

Lets get started!

Install JDK 9

  1. Download the latest build, 61 at the time of this writing.
  2. Setup JAVA_HOME as:
    More details on setting JAVA_HOME on OSX are here.
  3. Verify the version:

Checkout and Install jline2

jline2 is a Java library for handling console input. Check it out:

And then build it:

Clone and Build JDK 9 REPL

OpenJDK codename for the project is Kulla which means “The God of Builders”. Planned name for the tool is jshell.

  1. Check out the workspace:
  2. Get the sources:
  3. Edit langtools/repl/scripts/compile.shscript such that it looks like:
    Notice, the only edits are #!/bin/sh for OSX and adding JLINE2LIB to the location of your previously compiled jline2 workspace. javac is picked from JAVA_HOME that is referring to JDK 9.
  4. Compile the REPL tool by invoking the script from langtools/repl directory:

Run JDK 9 REPL

  1. Edit langtools/repl/scripts/run.sh script such that it looks like:
    Notice, the only edits are !/bin/sh for OSX and adding JLINE2LIB.
  2. Run REPL as:

JDK 9 REPL Hello World

Unlike the introduction of bouncing ball or dancing Duke that was used to introduce Java, we’ll just use the conventional Hello World for REPL

Run “Hello World” as:

Voila!

No public static void main, no class creation, no ceremony, just clean and simple Java code. The entered text is called as “snippet”.

The complete Java code can be seen using /list all and looks like:

This snippet can be saved to a file as:

Note this is not a Java file. Saved snippet is exactly what was entered:

And the tool can be exited as:

Or you can just hit Ctrl+C.

Complete list of commands can be easily seen:

JDK 9 REPL Next Steps and Feedback

Follow the REPL Tutorial to learn more about the tool’s capability. Here is a quick overview:

  • Accepts Java statements, variable, method, and class definitions, imports, and expressions
  • Commands for settings and to display information, such as /list to display the list of snippets, /vars to display the list of variables, /save to save your snippets, /open to read them back in.
  • History of snippets is available, snippets can be edited by number, and much more

Here is an RFE that would be useful:

  • Export a snippet as full blown Java class

A subsequent blog will showcase how this could be used for playing with a Java EE application. How would you use REPL?

Discuss the project/issues on kulla-dev.

Keep Calm and REPL

Enjoy!