---
title: Notes on Using Java Kernel with Jupyter
tags: ["Java", "Python", "Jupyter", "Machine Learning"]
categories: ["Dev", "MachineLearning", "Jupyter", "Java"]
date: 2023-09-01T03:31:01Z
updated: 2023-09-01T03:39:04Z
---

> ⚠️ This article was automatically translated by OpenAI (gpt-4o).
> It may be edited eventually, but please be aware that it may contain incorrect information at this time.

Jupyter provides an interactive computing environment. It's usually used with Python, but I found it can also be used with Java, so here are my notes.

You need to have jshell available. I tested with the following Java version.

```
$ java -version
java version "17.0.7" 2023-04-18 LTS
Java(TM) SE Runtime Environment Oracle GraalVM 17.0.7+8.1 (build 17.0.7+8-LTS-jvmci-23.0-b12)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.7+8.1 (build 17.0.7+8-LTS-jvmci-23.0-b12, mixed mode, sharing)

$ java --list-modules | grep "jdk.jshell"
jdk.jshell@17.0.7
```

### Setting up Python

I'm not very familiar with Python, so I'm not sure if this setup method is correct.

Install pyenv

```
brew install xz
brew install pyenv

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

source ~/.zshrc

pyenv install 3.11.5
```

From here, work in `~/jupyterlab-java`

```
mkdir -p ~/jupyterlab-java
cd ~/jupyterlab-java
pyenv local 3.11.5
```

Create an environment with venv

```
python3 -m venv .venv
source .venv/bin/activate
```

### Install jupyter

```
pip3 install jupyter
```

### Install Java kernel

```
git clone https://github.com/frankfliu/IJava.git
cd IJava
./gradlew -q installKernel
cd ..
```

Check the list of kernels

```
$ jupyter kernelspec list
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Available kernels:
  python3    /Users/tmaki/jupyterlab-java/.venv/share/jupyter/kernels/python3
  java       /Users/tmaki/Library/Jupyter/kernels/java
```

### Launch Jupyter Lab

```
jupyter lab
```

Select Java Notebook

<img width="1912" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/44981c1f-e076-4c30-adf2-698773484cd6">

You can write and execute Java code interactively.

<img width="1912" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/ba182d44-e7a2-4360-8cde-b54ec12384a9">

Define a class with Record

<img width="1912" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/e87ddf9a-9ec7-468b-aad0-a96ebf118636">

You can download libraries from the Maven Repository.

https://github.com/SpencerPark/IJava/blob/master/docs/magics.md#addmavendependencies

<img width="1912" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/aab5c2a1-a773-4ffa-8bbf-ebbc90933b15">

Auto-completion works with the `TAB` key.

<img width="1912" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/8afcce51-f237-4f63-a7dc-ded2de9e1451">

You can also draw graphs

<img width="1868" alt="image" src="https://github.com/making/blog.ik.am/assets/106908/d7ccbaa2-031a-4eb7-bd6a-4b1c848c3f05">

The ipynb file used above is [here](https://gist.github.com/making/4708e293f75a6f4d0d9dfaa14d8b6580)

---

Speaking of Jupyter, it's often associated with machine learning, so I plan to try [JDL](https://github.com/deepjavalibrary/djl) and [Tribuo](https://tribuo.org/).

> I wanted to use it on Colaboratory, but the connection to the Java runtime remains "connecting" and doesn't work. It seems there are some cases where it works, though...
