Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manisnesan/e23822bbc4889345330d201dea00f286 to your computer and use it in GitHub Desktop.
Save manisnesan/e23822bbc4889345330d201dea00f286 to your computer and use it in GitHub Desktop.
Running Java in Jupyter Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "28e28f88-5db7-4cab-b004-7cc3bc745c68",
"metadata": {},
"source": [
"# Running Java using Jupyter notebooks\n",
"\n",
"## Requirements\n",
"\n",
"- Java JDK >= 9 (1. Install Java >= 9. In my case I installed java using [sdk](https://sdkman.io/). ` $ sdk install java 17.ea.3.pma-open` 2. Verify java version)\n",
"- Juputer-like enviornment (1. Install a virtual environment using conda `conda create -n ijava python=3.9` 2. Activate the created environment `conda activate ijava` 3. Install jupyter lab `conda install -c conda-forge jupyter-lab`.\n",
"\n",
"## Setup\n",
"\n",
"- Download the java kernel from [IJava](https://github.com/SpencerPark/IJava) - (`wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip`) \n",
"- Unzip the directory\n",
"- Install the java kernel `python -m install.py --user`\n",
"- Validate the kernel `jupyter kernelspec list java`\n",
"\n",
"## Running\n",
"- Start the Jupyter lab `jupyter lab`"
]
},
{
"cell_type": "markdown",
"id": "1ef5be5d-3e0f-4762-b411-8821d6cbfcff",
"metadata": {},
"source": [
"## Running the Java Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "94c4202e-e682-4b99-95bb-e88d71881e11",
"metadata": {},
"outputs": [],
"source": [
"double a = 4.0;"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "28b8d199-1fba-4409-ab88-0db39a4121c0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.7568024953079282"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Math.sin(a)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1973aad3-6150-4f31-8200-65c4b230b588",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Hello World This is a jupyter notebook."
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var sb = new StringBuilder();\n",
"\n",
"sb.append(\"Hello World\").append(\" This is a jupyter notebook.\");"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1a84ad46-fe31-4182-b65e-0633fff94492",
"metadata": {},
"outputs": [],
"source": [
"var sb = new StringBuilder();"
]
},
{
"cell_type": "markdown",
"id": "cd29bdee-13dc-4c27-8977-003447d88811",
"metadata": {},
"source": [
"## Adding maven dependencies at runtime"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "09867309-47df-493a-a55b-164bb066cfb9",
"metadata": {},
"outputs": [],
"source": [
"%mavenRepo oss-sonatype-snapshots https://oss.sonatype.org/content/repositories/snapshots/"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "88f8d414-da72-4d6b-9fd5-dc12a76630ad",
"metadata": {},
"outputs": [],
"source": [
"%maven io.github.spencerpark:jupyter-jvm-basekernel:2.0.0-SNAPSHOT"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "dafa8340-b520-43b6-8b9f-b22024ca9cfe",
"metadata": {},
"outputs": [],
"source": [
"%jars *.jar"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "8252576c-4518-485b-b4de-210b42e5f204",
"metadata": {},
"outputs": [],
"source": [
"%%loadFromPOM\n",
"<repository>\n",
" <id>oss-sonatype-snapshots</id>\n",
" <url>https://oss.sonatype.org/content/repositories/snapshots/</url>\n",
"</repository>\n",
"\n",
"<repository>\n",
" <id>oss-sonatype-releases</id>\n",
" <url>https://oss.sonatype.org/content/repositories/releases/</url>\n",
"</repository>\n",
"\n",
"<dependency>\n",
" <groupId>io.github.spencerpark</groupId>\n",
" <artifactId>jupyter-jvm-basekernel</artifactId>\n",
" <version>2.0.0-SNAPSHOT</version>\n",
"</dependency>\n",
"\n",
"<dependency>\n",
" <groupId>org.apache.commons</groupId>\n",
" <artifactId>commons-math3</artifactId>\n",
" <version>3.6.1</version>\n",
"</dependency>"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "0948ea82-4723-4505-a4be-6d0ca95f3929",
"metadata": {},
"outputs": [],
"source": [
"%classpath"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "49e8b28b-280f-4e1f-8b0c-81942da84da3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"80.0"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import org.apache.commons.math3.fraction.*;\n",
"Fraction.FOUR_FIFTHS.percentageValue()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1bbf8bb-7473-4318-8c51-0902d8f6b7a5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17-panama+3-167"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment