avatarGary Sharpe

Summary

This context provides a guide on how to install and use the Java kernel (IJava) in Google Colab for Jupyter notebooks.

Abstract

The given context discusses the use of Java with Jupyter notebooks and introduces the IJava kernel, which can be installed on Google Colab. Google Colab is a hosted Jupyter notebook service that offers an easy-to-use development platform for sharing and replicating work. The article provides step-by-step instructions for installing the Java kernel on Google Colab, including downloading, editing, and uploading the .ipynb file, and executing code blocks. The guide also mentions the use of Apache Maven for importing dependencies using magic commands provided by the IJava kernel.

Opinions

  • The author finds Google Colab to be a useful tool for writing Java in Jupyter notebooks.
  • The author recommends using source control (GitHub) and Jupyter Notebooks to teach Java.
  • The author finds Jupyter Notebooks to be an effective tool for teaching, presenting new architectures or proof of concepts, and sharing easily reproducible research.
  • The author encourages the use of the IJava kernel for writing Java in Jupyter notebooks.
  • The author suggests using Apache Maven for importing any dependencies using magic commands provided by the IJava kernel.
  • The author emphasizes the importance of showing one's work and encourages creativity.
  • The author is open to hearing from anyone exploring the use of Java in the context of Jupyter notebooks and on the command line for data processing and visualization.

Java, Jupyter and Google Colab

Java and Jupyter

Although there are just as many reasons to use Java with Jupyter as there are any other language, I’m particularly fond of using the duo to teach, present new architectures or proof of concepts, and share easily reproducible research.

The blog post titled “Teaching Java with Jupyter Notebooks”, by Nicolas Frankel, presents a concise & relatable argument for using source control (GitHub) and Jupyter Notebooks to teach Java.

  • Easy to setup and maintain
  • Rich text & formatting, including support for html, css, LaTeX, and more.
  • Reproducible (and interactive) visualizations
  • Interactive and Extensible Code

In the words of the Jupyter maintainers themselves,

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.

Google Colab & IJava

There are a couple notable options for writing Java in Jupyter Notebooks. Though not exhaustive, the list includes IJava, SciJava & BeakerX. Each represent a slightly different approach.

In this article, I’ll demonstrate how to install the IJava kernel on a Google Colab Notebook.

Google Colab is a hosted Jupyter Notebook service, providing a development platform that can be used to easily share and replicate work. There’s very little required to get started, and with access to the underlying container, there are countless applications.

If you’re not already familiar with Colab, I strongly recommend creating an account and working through some of the examples in the official docs.

The ‘Duke’. Image from Wikimedia.org

Installing the Kernel

Although Colab is geared and promoted predominantly as a tool for writing Python, adding a Java Kernel to is actually pretty simple.

Assuming you know how to create a new account, and can navigate creating a new notebook…

  1. Download the .ipynb under ‘file’ - ’download’ - ‘.ipynb’
  2. Edit the ‘.ipynb’ file directly using something like Notepad, Sublime, etc.

Under kernelspec.name, kernelspec.display_name & language_info.name replace the value with java

3. Next, upload changes (see below) and open this file in Google Colab.

4. Create a code block, add the following, and then execute <ctl. enter>

NOTE: replace ‘v1.3.0/ijava-1.3.0.zip’ above with the latest release, if relevant.

A view from Google Colab. Complete Script above and linked here as a GitHub Gist

5. Select ‘Connect to a hosted runtime’ near the top right of the Google Colab notebook editor.

6. Code!

For more information about the IJava Kernel in particular, go to:

Alternatively…

To skip a few steps above, you can open the following ‘blank’ notebook in Colab. Click on the image below to go to GitHub Gists, and then click on the Open in Colab icon.

Example Blank Notebook with Java Kernel configuration in the document’s “kernelspec”

Next, pick up with step #4 above to complete the IJava installation and configuration.

What’s Next? … Magics!

One particularly useful tool allows us to leverage Apache Maven for importing any dependencies using something like the following, which you can enter into a Notebook code block and execute <ctl. enter>:

%maven org.knowm.xchart:xchart:3.5.2

This is a form of ‘magic’ provided by the IJava Kernel.

These are very similar to the ‘magics’ you would otherwise find in IPython.

After executing the above, or something comparable,%maven groupId:artifactId:version, you can leverage the library in your code blocks:

That’s it!

If you would like to explore the intersection of Java, Jupyter & many of the available tools for Data Science, Engineering & general Analytics, below are a few more articles that may be of interest.

I look forward to seeing what all of you come up with next.

So, get creative, and don’t forget to Show Your Work!!

Jupiter, from Pixabay

Note from the Author

I’d love to hear back from anyone exploring how to use Java in the context of Notebooks (Jupyter, Zeppelin, etc.) and on the command line (JShell) for data processing and visualization using tools like Tablesaw.

Shoot me a message, especially if you have published any material on Medium, and I’ll be happy to link to your content in the future, when relevant.

Please consider signing up for a Medium membership with the above member Referral Link — Thank You!
Java
Jupyter Notebook
Jupyter Kernel
Google Colab
Data Science
Recommended from ReadMedium