The Java 9 'Kulla' REPL

Maybe it’ll be part of JDK 9, maybe it won’t… but people are working hard on creating a REPL tool/environment for the Java Development Kit (JDK). More information on the code and the project is available as part of OpenJDK: project Kulla.

Some of you might not be familiar with the terminology ‘REPL’, it is short for Read Evaluate Print Loop. It is hard to explain exactly what it does, but very easy to demonstrate:

|  Welcome to the Java REPL mock-up -- Version 0.23
|  Type /help for help

-> System.out.println("Hello World!");
Hello World!

->

The idea is that you have a place you can enter Java and run it, without main method, class structure. You can build instances, and alter them while you type. For example you’ll able to do Swing development while you type:

-> import javax.swing.*;
-> JFrame frame = new JFrame();
-> frame.setVisible(true);

Now we have a visible frame, you can drag it around, resize it etc.

-> JPanel panel = new JPanel();
-> frame.add(panel);
-> JButton button = new JButton();
-> panel.add(button);

Suddenly our frame has a panel, and the panel has an empty button! You can prototype, do live coding and you have instant feedback.

-> button.setText("Push me!");

Now the button has text, but pressing it still does nothing…

Push me!

-> button.addActionListener(e -> System.out.println("Hello World!"));
-> 
Hello World!
Hello World!
Hello World!

And there we go, a final simple lambda creates a working “Hello World!”-button.

It is also possible to load a repl script from file, allowing you to share, store and run scripts. This is done using the ‘/load’ and ‘/save’ commands. You can even ‘/dump’ all the created classes in a directory.

I’m very curious how people will be using the REPL in the future, some use cases:

If you want to try out Kulla, it took me literally 20 minutes to get up and running on my MacBook. Just follow the instructions on AdoptOpenJDK, but instead use http://hg.openjdk.java.net/kulla/dev/ as codebase. After building the JDK, go to ./langtools/repl and look at the README.