Skip to main content

Magic cell para ejecutar Java de forma interactiva en Jupyter y Colab

Project description

Java Magic for Google Colab

A lightweight Notebook extension to write, compile, and execute Java code directly inside cells. It features automatic Google Colab setup and real-time interactive console input support (Scanner, System.in).

🛠️ Installation

pip install java-magic

🚀 Quick Start

  1. Load the extension In your first notebook cell, load the magic:
%load_ext java_magic
  1. Run standard Java code You don't even need a main class wrapper for simple snippets; the extension handles it automatically:
%%java
System.out.println("Hello from Jupyter!");

💡 Tip: Syntax Highlighting

By default, Jupyter notebooks might not highlight Java code inside custom magic cells. To enable syntax highlighting, simply add %%js right next to the magic command like this:

%%java %%js
System.out.println("This code will be colored!");
  1. Run interactive Java code If the extension detects Scanner or System.in, it automatically switches to an interactive terminal loop: (Tip: Add %js to )
%%java %%js
import java.util.Scanner;

Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
  1. Multi-cell Execution You can define a class in one cell and use it in another!

    Cell 1: Define a class

%%java %%js
package math;

/**
 * Clase de utilidad para operaciones matemáticas básicas.
 */
public class Calculadora {

    /**
     * Suma dos números enteros.
     * @param a El primer operando.
     * @param b El segundo operando.
     * @return La suma de a y b.
     */
    public static int sumar(int a, int b) {
        return a + b;
    }
}

Cell 2: Use the class

%%java %%js
import math.Calculadora;

int x = 10;
int y = 25;
int resultado = Calculadora.sumar(x, y);

System.out.println("El resultado de la suma es: " + resultado);

📚 Generate JavaDoc

You can easily render interactive HTML documentation right inside your notebook using the %javadoc magic command.

By default, the extension automatically deletes generated .java files after compilation to keep your workspace clean. Only the compiled .class files are preserved so you can use them across different cells.

However, if you want to generate documentation using Javadoc, the source code must be present in the directory.

%config JavaMagics.remove_java_file = False

Run your cell normally (e.g., the one containing math.Calculadora). Since the configuration was changed, both the .class and the .java files will now be saved in your environment.

Now you can pass the path of the .java file to the %javadoc line magic to display the interactive, scrollable HTML documentation directly inside your notebook:

%javadoc math/Calculadora.java

🧠 Technical Details & Architecture

When you run a cell, the extension creates physical .java and .class files in your environment's current working directory (e.g., Google Colab's /content). You can verify they exist by running:

!ls -R | grep -E "class|java"

How it differs from IJava Unlike the IJava kernel, this extension is not a continuous REPL. State is not saved: Variables declared in one cell (e.g., int x = 5;) do not persist in memory for the next cell.

📄 License MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

java_magic-0.1.6.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

java_magic-0.1.6-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file java_magic-0.1.6.tar.gz.

File metadata

  • Download URL: java_magic-0.1.6.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for java_magic-0.1.6.tar.gz
Algorithm Hash digest
SHA256 d0e40da9a12237a9d8e407f84a24d0ce7e56af4e18ac9ec1b97658d185c605fd
MD5 126e96eac61a977c5b9839b0802b5b5d
BLAKE2b-256 68dd392eb1f011f40f2971ae3b9c9017ed12b0fd1d68e2183a2808327cd8eeb3

See more details on using hashes here.

File details

Details for the file java_magic-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: java_magic-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for java_magic-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2efee997136ddd2cba704f4f60fec66920c9d73e92e46ef7a4c73bee34fbbed2
MD5 6089b9961af97f36e8c02871933a11ae
BLAKE2b-256 e1503fd56198b914d441cb185893890b6237d71b093ef6cd70be6ed0e8519424

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page