Java9 via JShell (aka Kulla)

Based on https://github.com/Bachmann1234/java9_kernel

With changes, added MetaKernel and other fixes.

For more help, see: https://java.net/downloads/adoptopenjdk/REPL_Tutorial.pdf

In [1]:
int x = 1;
 int x = 1;
|  Added variable x of type int with initial value 1


In [2]:
y
 y
|  Error:
|  cannot find symbol
|    symbol:   variable y
|  y
|  ^


In [3]:
void draw() {
}
 }
|  Added method draw


In [27]:
int test_variable_name = 1;
 int test_variable_name = 1;
|  Added variable test_variable_name of type int with initial value 1


Return values

Notice that these expressions have return values (Out):

In [4]:
x
 x
|  Variable x of type int has value 1


Out[4]:
1
In [5]:
1
 1
|  Expression value is: 1
|    assigned to temporary variable $1 of type int


Out[5]:
1
In [6]:
$1 + 42
 $1 + 42
|  Expression value is: 43
|    assigned to temporary variable $2 of type int


Out[6]:
43
In [7]:
"this is a test"
 "this is a test"
|  Expression value is: "this is a test"
|    assigned to temporary variable $3 of type String


Out[7]:
'this is a test'
In [8]:
List list = new ArrayList();
list.add(1);
list;
 list;
|  Variable list of type List has value [1]


Out[8]:
[1]

Magics

Building the Java9 kernel around MetaKernel gives you access to magics and the shell.

Here are some examples showing access to the shell and other magics:

In [9]:
! ls
Calysto Simulator.ipynb      evolved.py			 sim-movie.gif
conx.py			     IVisual VPython Demo.ipynb  simulation.png
Dot Flowchart Example.ipynb  Java9.ipynb		 World.java
eipd.py			     Numeric.py

In [10]:
%%file World.java
    
System.out.println("World!");
Created file '/home/dblank/public_html/Experiments/World.java'.
In [11]:
%include World.java
 System.out.println("World!");
World!


In [10]:
%load World.java
In [12]:
System.out.println("World!");
 System.out.println("World!");
World!


In [13]:
%%shell

ls
Calysto Simulator.ipynb      evolved.py			 sim-movie.gif
conx.py			     IVisual VPython Demo.ipynb  simulation.png
Dot Flowchart Example.ipynb  Java9.ipynb		 World.java
eipd.py			     Numeric.py

Parallel Magics

One very powerful magic is being able to run multiple kernels through a simple interface.

First we start up a cluster (dashboard -> Cluster -> start)

Next, we use the %parallel magic:

In [14]:
%parallel javakernel JavaKernel
Waiting for engines...

Finally, we execute a Java program in parallel, getting back N results (just two in this case):

In [15]:
%%px 

42
Out[15]:
[42, 42]

Kernel Magics

Also, because JavaKernel is a MetaKernel, from any other MetaKernel instance, we can run Java code:

In [16]:
%kernel javakernel JavaKernel
Out[16]:
<javakernel.kernel.JavaKernel object at 0x7f4940d7fcc0>
In [17]:
%kx Math.sqrt(2);
 Math.sqrt(2);
|  Expression value is: 1.4142135623730951
|    assigned to temporary variable $1 of type double


Out[17]:
1.4142135623730951

JShell commands

In [18]:
/vars
 /vars
|    boolean $4 = true
|    int x = 1
|    List list = [1]
|    int $1 = 1
|    int $2 = 43
|    String $3 = "this is a test"


In [19]:
/methods
 /methods
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    draw ()V


In [20]:
/list all
 /list all

   1 : import java.util.*;
   2 : import java.io.*;
   3 : import java.math.*;
   4 : import java.net.*;
   5 : import java.util.concurrent.*;
   6 : import java.util.prefs.*;
   7 : import java.util.regex.*;
   8 : void printf(String format, Object... args) { System.out.printf(format, args); }
   9 : int x = 1;
  10 : void draw() {
       }
  11 : x
  12 : 1
  13 : $1 + 42
  14 : "this is a test"
  15 : List list = new ArrayList();
  16 : list.add(1);
  17 : list;
  18 : System.out.println("World!");
  19 : System.out.println("World!");


In [21]:
// Comment
class Test {
    public static void main() {
        System.out.println("Hallooo");
    }
}
 }
|  Added class Test


In [22]:
Test test = new Test();
test.main();
 test.main();
Hallooo


In [23]:
/classes
 /classes
|    class Test


In [24]:
/methods
 /methods
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    draw ()V


In [25]:
/vars
 /vars
|    boolean $4 = true
|    Test test = Test@6956de9
|    int x = 1
|    List list = [1]
|    int $1 = 1
|    int $2 = 43
|    String $3 = "this is a test"


Command-line Completions

Press TAB after a few letters for command-line completion:

  • type a few letters
  • press TAB
  • if only one choice, it completes; more options give popup list
  • pick one

In [ ]:
// type t<TAB>
t