Last active
May 1, 2025 18:09
-
-
Save xarical/740db474f77c4f042d175c5af1eaab96 to your computer and use it in GitHub Desktop.
Get started with using Java in Google Colab (iPython-like through jupyter-java and running files through the jdk)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOQcYBkik8GeojTHTK9gUX+", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/xarical/740db474f77c4f042d175c5af1eaab96/java-google-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Run in iPython" | |
| ], | |
| "metadata": { | |
| "id": "UJn7V1RTRXpT" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "WZh-db-ydhSQ" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Install jupyter-java\n", | |
| "!pip install jbang\n", | |
| "import jbang\n", | |
| "jbang.exec(\"trust add https://github.com/jupyter-java\")\n", | |
| "jbang.exec(\"install-kernel@jupyter-java\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "// Change runtime type to java (JJava/j!)\n", | |
| "// Run java code in the Colab notebook\n", | |
| "System.out.println(\"Hello world!\");" | |
| ], | |
| "metadata": { | |
| "id": "o2czkCQK5E3M" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Or, run files" | |
| ], | |
| "metadata": { | |
| "id": "_H3LKTxtRa2q" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Install the jdk\n", | |
| "!apt-get install default-jdk > /dev/null" | |
| ], | |
| "metadata": { | |
| "id": "5K6xZXuu32Kj" | |
| }, | |
| "execution_count": 1, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Make directory (files go in here)\n", | |
| "!mkdir Project" | |
| ], | |
| "metadata": { | |
| "id": "ea-jYgVQZXkZ" | |
| }, | |
| "execution_count": 2, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Compile the program\n", | |
| "!javac -cp . Project/*.java" | |
| ], | |
| "metadata": { | |
| "id": "Nf-tEGYc5C4o" | |
| }, | |
| "execution_count": 6, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Run the program\n", | |
| "!cd Project && java -cp . MyProgram" | |
| ], | |
| "metadata": { | |
| "id": "JLSywAoj1imO" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment