{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Java9 via JShell (aka Kulla)\n", "\n", "Based on https://github.com/Bachmann1234/java9_kernel\n", "\n", "With changes, added MetaKernel and other fixes.\n", "\n", "For more help, see: https://java.net/downloads/adoptopenjdk/REPL_Tutorial.pdf" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " int x = 1;\n", "| Added variable x of type int with initial value 1\n", "\n", "\n" ] } ], "source": [ "int x = 1;" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " y\r\n", "| Error:\r\n", "| cannot find symbol\r\n", "| symbol: variable y\r\n", "| y\r\n", "| ^\r\n", "\r\n", "\n" ] } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " }\n", "| Added method draw\n", "\n", "\n" ] } ], "source": [ "void draw() {\n", "}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " int test_variable_name = 1;\n", "| Added variable test_variable_name of type int with initial value 1\n", "\n", "\n" ] } ], "source": [ "int test_variable_name = 1;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Return values\n", "\n", "Notice that these expressions have return values (Out):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " x\n", "| Variable x of type int has value 1\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1\n", "| Expression value is: 1\n", "| assigned to temporary variable $1 of type int\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " $1 + 42\n", "| Expression value is: 43\n", "| assigned to temporary variable $2 of type int\n", "\n", "\n" ] }, { "data": { "text/plain": [ "43" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "$1 + 42" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \"this is a test\"\n", "| Expression value is: \"this is a test\"\n", "| assigned to temporary variable $3 of type String\n", "\n", "\n" ] }, { "data": { "text/plain": [ "'this is a test'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"this is a test\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " list;\n", "| Variable list of type List has value [1]\n", "\n", "\n" ] }, { "data": { "text/plain": [ "[1]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "List list = new ArrayList();\n", "list.add(1);\n", "list;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Magics\n", "\n", "Building the Java9 kernel around MetaKernel gives you access to magics and the shell.\n", "\n", "Here are some examples showing access to the shell and other magics:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calysto Simulator.ipynb evolved.py\t\t\t sim-movie.gif\r\n", "conx.py\t\t\t IVisual VPython Demo.ipynb simulation.png\r\n", "Dot Flowchart Example.ipynb Java9.ipynb\t\t World.java\r\n", "eipd.py\t\t\t Numeric.py\r\n", "\n" ] } ], "source": [ "! ls" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/Experiments/World.java'.\n" ] } ], "source": [ "%%file World.java\n", " \n", "System.out.println(\"World!\");" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " System.out.println(\"World!\");\n", "World!\n", "\n", "\n" ] } ], "source": [ "%include World.java" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%load World.java" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " System.out.println(\"World!\");\n", "World!\n", "\n", "\n" ] } ], "source": [ " \n", "System.out.println(\"World!\");" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calysto Simulator.ipynb evolved.py\t\t\t sim-movie.gif\r\n", "conx.py\t\t\t IVisual VPython Demo.ipynb simulation.png\r\n", "Dot Flowchart Example.ipynb Java9.ipynb\t\t World.java\r\n", "eipd.py\t\t\t Numeric.py\r\n", "\n" ] } ], "source": [ "%%shell\n", "\n", "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parallel Magics\n", "\n", "One very powerful magic is being able to run multiple kernels through a simple interface.\n", "\n", "First we start up a cluster (dashboard -> Cluster -> start)\n", "\n", "Next, we use the %parallel magic:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Waiting for engines...\n" ] } ], "source": [ "%parallel javakernel JavaKernel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we execute a Java program in parallel, getting back N results (just two in this case):" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[42, 42]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%px \n", "\n", "42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kernel Magics\n", "\n", "Also, because JavaKernel is a MetaKernel, from any other MetaKernel instance, we can run Java code:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%kernel javakernel JavaKernel" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Math.sqrt(2);\n", "| Expression value is: 1.4142135623730951\n", "| assigned to temporary variable $1 of type double\n", "\n", "\n" ] }, { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%kx Math.sqrt(2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JShell commands" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /vars\n", "| boolean $4 = true\n", "| int x = 1\n", "| List list = [1]\n", "| int $1 = 1\n", "| int $2 = 43\n", "| String $3 = \"this is a test\"\n", "\n", "\n" ] } ], "source": [ "/vars" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /methods\n", "| printf (Ljava/lang/String;[Ljava/lang/Object;)V\n", "| draw ()V\n", "\n", "\n" ] } ], "source": [ "/methods" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /list all\n", "\n", " 1 : import java.util.*;\n", " 2 : import java.io.*;\n", " 3 : import java.math.*;\n", " 4 : import java.net.*;\n", " 5 : import java.util.concurrent.*;\n", " 6 : import java.util.prefs.*;\n", " 7 : import java.util.regex.*;\n", " 8 : void printf(String format, Object... args) { System.out.printf(format, args); }\n", " 9 : int x = 1;\n", " 10 : void draw() {\n", " }\n", " 11 : x\n", " 12 : 1\n", " 13 : $1 + 42\n", " 14 : \"this is a test\"\n", " 15 : List list = new ArrayList();\n", " 16 : list.add(1);\n", " 17 : list;\n", " 18 : System.out.println(\"World!\");\n", " 19 : System.out.println(\"World!\");\n", "\n", "\n" ] } ], "source": [ "/list all" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " }\n", "| Added class Test\n", "\n", "\n" ] } ], "source": [ "// Comment\n", "class Test {\n", " public static void main() {\n", " System.out.println(\"Hallooo\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " test.main();\n", "Hallooo\n", "\n", "\n" ] } ], "source": [ "Test test = new Test();\n", "test.main();\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /classes\n", "| class Test\n", "\n", "\n" ] } ], "source": [ "/classes" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /methods\n", "| printf (Ljava/lang/String;[Ljava/lang/Object;)V\n", "| draw ()V\n", "\n", "\n" ] } ], "source": [ "/methods" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /vars\n", "| boolean $4 = true\n", "| Test test = Test@6956de9\n", "| int x = 1\n", "| List list = [1]\n", "| int $1 = 1\n", "| int $2 = 43\n", "| String $3 = \"this is a test\"\n", "\n", "\n" ] } ], "source": [ "/vars" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Command-line Completions\n", "\n", "Press TAB after a few letters for command-line completion:\n", "\n", "* type a few letters\n", "* press TAB\n", "* if only one choice, it completes; more options give popup list \n", "* pick one" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "// type t\n", "t" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }