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.

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 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!

Be Sociable, Share!

13 thoughts on “JDK 9 REPL: Getting Started (Tech Tip #87)

  1. hi arun,

    am interacting with you again after a while (after you transition to oracle and then to REDHAT).

    I have a windows box. Now, how do I build kulla on windows ? Isn’t there a getsources.bat file ?

    thank you,

    BR,
    ~A

  2. Lovely, that worked like a charm:

    docker run -it java:9 jshell
    Unable to find image ‘java:9’ locally
    9: Pulling from library/java
    fd8abc45a665: Pull complete
    6b9443a233f0: Pull complete
    0335de578c48: Pull complete
    cba95d080490: Pull complete
    626da3191cff: Pull complete
    e3359378c8f8: Pull complete
    82896c1f6dcb: Pull complete
    1cfe40c802a8: Pull complete
    b2dea43f58ba: Pull complete
    680dba759456: Pull complete
    b2a90556d549: Pull complete
    d7b4897e4bdf: Pull complete
    Digest: sha256:c971300628da39afa6df3dcafd9d88f90d85530d1f4e763a010dbd7524edce20
    Status: Downloaded newer image for java:9
    Dec 16, 2015 6:37:36 PM java.util.prefs.FileSystemPreferences$1 run
    INFO: Created user preferences directory.
    | Welcome to JShell — Version 1.9.0-internal
    | Type /help for help

Leave a Reply

Your email address will not be published. Required fields are marked *