Magic cell para ejecutar Java de forma interactiva en Jupyter y Colab
Project description
Jupyter Java Magic
A lightweight Jupyter 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
- Load the extension In your first notebook cell, load the magic:
%load_ext java_magic
- 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!");
- Run interactive Java code
If the extension detects Scanner or System.in, it automatically switches to an interactive terminal loop:
(Tip: Add
%jsto )
%%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 + "!");
-
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 Documentation
You can easily render interactive HTML documentation right inside your notebook using the %javadoc magic command:
%javadoc math/Calculadora.java
⚙️ Configuration
By default, generated .java files are kept in your directory. To change this behavior, you can configure the extension:
%config JavaMagics.remove_java_file = True
To use %%javadoc set remove_java_file to False, since it needs the .java file to generate the documentation.
🧠 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 *.java *.class
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file java_magic-0.1.4.tar.gz.
File metadata
- Download URL: java_magic-0.1.4.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65acf4a11327f05b5e20df6bc8540fc5b7f713a36aa7a11576a17136ea87c1fd
|
|
| MD5 |
6dcc7ca65cef8c2c8ca2fb8686dc0f5a
|
|
| BLAKE2b-256 |
c7baf001abaefeda18803b0c9e3396cfcd299ebcec38dcc42e5658f4684c92ea
|
File details
Details for the file java_magic-0.1.4-py3-none-any.whl.
File metadata
- Download URL: java_magic-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dbcc8aa114c34145cc6bab7f40cbc1cff87665c55028b3c421ddf872b32275a
|
|
| MD5 |
ca346003c8f26f344484f6058f903377
|
|
| BLAKE2b-256 |
3d0e221f162745fd223e558c1fec540ddc2347e60d435faa1fbb12b48d7ff234
|