Introduction to Java Notebooks

Introduction to Java Notebooks

Java Notebook is a new and exciting way to explore the Java programming language. It works a lot like Jupyter Notebooks.

Notebooks are a community standard for communicating and performing interactive computing. They are a document that blends computations, outputs, explanatory text, mathematics, images, and rich media representations of objects.

It allows you to interleave blocks of text (in Markdown), editable and executable Java code blocks and output blocks.

[1] [Run] [Edit]

System.out.println("Hello world!");
> Hello world!

As you can see, when we run the code block above, the output is immediately updated. It is similar to JShell, but in the browser.

Maven support

Notebooks are an ideal tool to teach programming languages and to display and explore libraries and frameworks. Notebooks also support the import of external libraries using Apache Maven. Here is an example using the load command to import Apache Commons.

[2] [Run] [Edit]

load org.apache.commons:commons-lang3;
import org.apache.commons.lang3.StringUtils;

System.out.println(StringUtils.reverse("Hello world!"));
> !dlrow olleH

Data processing with jandas

It is also possible to directly import resources from internet and parse them to Java objects and records using frameworks like jandas (loosely based on pandas).

Here is an interactive example:

[3] [Run] [Edit]

load jandas:jandas:0.24.0;
import org.jandas.*;

record Song(int rank, String songName, long amountStreams, String artist, Date releaseDate) {}

var songs = CsvReader.read("https://www.royvanrijn.com/examples/data/spotify_top_1000.csv", Song.class);

System.out.println("Lady Gaga currently has " +
    songs.stream().filter(s -> s.artist.equals("Lady Gaga")).count() + " top 1000 hits.");

System.out.println("Ed Sheeran has " +
    songs.stream().filter(s -> s.artist.equals("Ed Sheeran")).mapToLong(s -> s.amountStreams).sum() + " total streams");

System.out.println("Bohemian Rhapsody was released: " +
    songs.stream().filter(s -> s.songName.equals("Bohemian Rhapsody")).map(s -> s.releaseDate).findAny());
> Lady Gaga currently has 6 top 1000 hits.
> Ed Sheeran has 26513653 total streams.
> Bohemian Rhapsody was released: Fri Oct 31 00:00:00 CET 1975

Hosting your own Notebooks

It is very easy to host your own notebooks. Users can run notebooks locally (using Docker) or use one of the following free cloud services to run your Java Notebook:

If you build or maintain a framework or library, or if you have a website explaining Java programming: Please add these interactive notebooks to your website. This makes it incredibly easy for new developers to get acquainted with your code.

Wake up!

Sadly (as you might have noticed) the examples above do not work. It was all just a dream. There is no such thing as a (widely used) Java Notebook.

The reason I wrote this post is the famous TIOBE Index of programming languages.

Today is the first day since 2001 that Java isn’t in the top 3.

That is why we, as the Java community, should look at our competition. What did Python do right? Why is it topping the chart?

A major reason, I think, is the feedback and steep learning curve of Java. If you want to parse some data, would you rather download a Java Runtime, install an IDE, install Maven etc? Or would you rather open the browser, have documentation and tutorials inlined with running examples? This is SO much easier.

The same is true for learning frontend/Javascript, there are so many websites that allow you to jump right in and test your code, sites like jsfiddle or codepen.

My wish for 2023: Oracle, Microsoft, Google, Azul… PLEASE make this a reality.

They do exist…

Update 2023-05-01:

Almost forgot: There are some online Java Notebooks that provide much of the functionality given above, here are some options you can consider:

  • Apache Zeppelin (a Jupyter-like runtime that has Java bindings)
  • IJava (a Jupyter kernel for executing Java code)
  • And more, mostly single developer hacked/weekend projects

The main problem I have isn’t that these project don’t exist; we just don’t utilize them, at all. There is no widespread support for this. It would be such a powermove if Oracle launched a similar product and encouraged libraries to embed it (running on their cloud for example).

If you want to get into AI/deep learning for example, here are two examples, Java or Python, I know which I’d pick:

Java: Quickstart DeepLearning4J:

Twenty steps of installing Java, Maven, git, IntelliJ, check out a project…

Python: Tensorflow Quickstart for Beginners:

No setup at all; from the start you’re importing Tensorflow and running the examples.

It isn’t all bad, here is an example where it does work:

Java: DJL

Easy to overlook, but press the top button “Run this notebook online”. Using IJava and Jupyter, yay!

Perfect for: https:://dev.java

I’d love for dev.java to become more engaging or interactive, something like a notebook. Have a place online where you can “Try now” without having to install everything.

Currently the Getting Started goes as follows:

  1. Open a plain text file, write your first class, without code formatting etc.
  2. Now you could compile it and run TADAA!
  3. But wait, you need to install a JDK first
  4. Download JDK
  5. Install JDK
  6. Learn to use the compiler
  7. Now run it.

Nobody got time for that anymore, sure, perhaps after you’ve decided to really go for it. But with notebooks you get instant results, you can tinker away, run stuff in the browser and see the results, perfect.

Anywaaaay, enough ranting for today ✌️