Thursday, September 19, 2019

[IJava] A Jupyter kernel for executing Java code

As we know that Jupyter’s is a Python-based solution, and it provides a lot of convenience for interactivity with Python code. But, I just wonder if Jupyter can use Java as its kernel, and it can. I find that it allows to integrate additional "kernels" with Java capabilities and someone has done it -IJava: https://github.com/SpencerPark/IJava


It requires:
1. Java 9
2. Jupyter notebooks

You can follow the IJava's offical document to install. Or, you also can build IJava Docker image for use.
Dockerfile
FROM openjdk:11.0.3-jdk

RUN apt-get update
RUN apt-get install -y python3-pip

# add requirements.txt, written this way to gracefully ignore a missing file
COPY . .
RUN ([ -f requirements.txt ] \
    && pip3 install --no-cache-dir -r requirements.txt) \
        || pip3 install --no-cache-dir jupyter jupyterlab

USER root

# Download the kernel release
RUN curl -L https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip > ijava-kernel.zip

# Unpack and install the kernel
RUN unzip ijava-kernel.zip -d ijava-kernel \
  && cd ijava-kernel \
  && python3 install.py --sys-prefix

# Set up the user environment

ENV NB_USER danny
ENV NB_UID 1000
ENV HOME /home/$NB_USER

RUN adduser --disabled-password \
    --gecos "Default user" \
    --uid $NB_UID \
    $NB_USER

COPY . $HOME
RUN chown -R $NB_UID $HOME

USER $NB_USER

# Launch the notebook server
WORKDIR $HOME
CMD ["jupyter", "notebook", "--ip", "*", "--port", "8888", "--no-browser"]

Build image and run this docker image
$ sudo docker build -t ijava:v0 .
$ sudo docker run -it --name ijava_danny \
    -v /home/test/danny/git:/home/danny/git \
    -p 8888:8888 ijava:v0

The following image is my testing on the jupyter with Java


P.S: The docker-compose example script
version: '2'
  services:
    ijava_danny:
      image: ijava:v0
      ports:
      - "8888:8888"
      volumes:
      - /home/liudanny/git:/home/liudanny/git
      restart: always

No comments: