Would you believe me if I said it was possible to run Java almost anywhere as long as you have access to Jupyter Notebooks ?
Jupyter Notebooks are an interactive computing environment that allows you to create and share documents that contain live code, equations, visualizations, and narrative text.
Notebooks always intrigued me as they allow for easy exploration. Java generally has not allowed for easy exploration in its early days. In recent years we have seen tools such as JBang and OpenJDK improvements that makes getting started with Java inherently simpler. For example single script files (JEP 330) and single main methods (JEP 445) have helped.
Jupyter Notebooks have started to become ubiquitous. I believe it’s valuable to make Java function seamlessly within Jupyter Notebooks or, at the very least, to understand how to use them together effectively. This article will provide insights into why integrating Java into Jupyter Notebooks is useful. Additionally, I will demonstrate how to enable Java in Jupyter on virtually any Jupyter-enabled platform, whether local or cloud-based. Finally – encourage you to checkout out https://github.com/jupyter-java where this story will hopefully evolve further.
So lets start with some of the reasons why Java in Jupyter Notebooks are interesting!
Interactive Learning and Explorative Development
Jupyter Notebooks provide an interactive interface where you can write and execute code in segments (called cells), see the output immediately, and make changes on the fly. This is different from the traditional Java development process, where you typically write a complete program, compile it, and then run it.
Not only does it let you experiment – it can also be used to let you setup notebooks that can interactively teach and educate others how to use the basics of Java or the API of your library.
Instead of just documenting how the API works one can put in snippets of code just next to the documentation that let you tinker and fix code to understand how it works. All without having to setup several layers of IDE and tooling to get started. That is powerful.
Support for Multiple Languages
Although Jupyter started with Python (hence the name, a play on Julia, Python, and R), it supports many languages by having Kernels that be written in and support any language, including Java.
Surprisingly to many there are already quite a list of Java based kernels. Especially for the non-Java JVM languages.
The main challenge with all these are that to install and enable them they each have their own install setup and not all “just works” in online IDE’s. Would it not be great to have a way to use these kernels anywhere?
Data Science and Visualization
Jupyter is popular in data science due to its support for data manipulation and visualization libraries. You can easily visualize data with graphs and charts, which is integrated directly in the notebook alongside the code that produces them.
Web-based and local IDE Interfaces
Jupyter runs in the browser, offering a web-based interface. This makes it platform-independent and accessible from anywhere. Jupyter style notebook are available in VSCode based IDE’s as well as Jupyter Labs online services as well as being rendered on GitHub – meaning Jupyter Notebook works locally and in the cloud – making notebooks ubiquitously available.
Documentation and Sharing
Notebooks allow you to combine code with Markdown text, equations written in LaTeX, and more. This makes it easy to create documents that explain the code and its output, which can be useful for educational purposes, note taking, data analysis reports, and more.
The key thing is to make these notebooks accessible with Java available so everyone can open and access them.
In conclusion, think of Jupyter Notebooks as an IDE where you can not only write and test your code in chunks but also document your process and visualize results in real-time, all within the same document.
Now how do you get to use Java in all these various notebook environments?
Until recently finding information about how to use Jupyter with Java been a challenge. Especially how to use the various java kernels and installing them in a way that worked across the various environments.
I created jupyter-java a Git Hub organization with its own landing page, discussion forum and a few repos all related to working with Java using Jupyter Notebooks. It is still in its infancy but contributions are very welcome.
I made it when I realized I could make Java Kernel installation a breeze compared to today’s complexity.
Easy Java Kernel Installation
This is available as a JBang script that you can run as follows:
By default it currently installs IJava based kernel but already supports installing the other following Kernels:
|Java, requires Java 21
|Java, Groovy, Kotlin
If you run
jbang install-kernel@jupyter-java <kernel name> and have VSCode with Jupyter Extension installed you can now open a New Jupyter Notebook and choose
Jupyter Kernel > jbang-<kernel name> to have that notebook working with the respective kernel.
The install-kernel script has various options to tweak the setup. For example if you want to be using latest greatest Java 21 preview features you can use:
jbang install-kernel@jupyter-java --java 21 —-enable-preview
And with that you can use String templates in your Jupyter Notebooks.
Java in any cloud Jupyter notebook
Notebooks are available in many places but they rarely if at all have Java enabled. With a little bit of additional JBang magic you can put the following into most Jupyter notebook environments and make a Java kernel with preview features available:
!pip install jbang
jbang.exec(“trust add https://github.com/jupyter-java”)
jbang.exec(“install-kernel@jupyter-java --java 21 —-enable-preview”)
Those four lines do the following:
- Install jbang-python, a Python module that installs JBang and provide API to use it from Python
- Import jbang module in Python
- Trust https://github.com/jupyter-java as a trusted url (an additional layer of protection so JBang doesn’t execute the code of a completely random stranger)
- Run install-kernel@jupyter-java to generate a Java Kernel setup.
All fairly simple stuff – but together it enables Java on many online services. Below are the various services it works on already today:
|Jupyter notebooks hosted by Google – just need a google account
|Free service that offer to run docker images with Jupyter included
|Red Hat Data Science
|Accessible for free via Red Hat OpenShift Sandbox – provides Jupyter notebooks
|GitPod.io VSCode online environment, has free tier
|GitHub codespaces based on VSCode, requires subscription – has OpenSource friendly free tier
|Red Hat OpenShift VSCode online environment, has free tier
If you want to try it out immediately go to github.com/jupyter-java/anywhere and use one of the direct links present in that repository.
Challenges and Future Prospects
My idea is that Jupyter-Java can if nothing else provide a starting point for Java developers to get started with Jupyter Notebooks. My hope is that it encourages you and others to evolve and improve the support for Java in Jupyter Notebooks as it is really nice to have available.
Maybe Java API’s will provide custom renderings ? Maybe a JBang enabled kernel is in the future? Maybe Jupyter Notebooks in Quarkus dev-ui would be possible ?
Lots of interesting opportunities.
Jupyter Notebooks and Java are possible and have multiple advantages and their combined use will help make Java more accessible and useful.
My encouragement to you in this jolly season is to go out and try Jupyter Notebooks – get started with https://github.com/jupyter-java/anywhere. With install-kernel@jupyter-java it is trivial to get started.
Explore the different Kernel variants, try use your favorite API’s, get a feel for it and then post questions or ideas in the discussions – lets spread the word and make the (Java) world a better place.