Skip to main content

java

Macro java 

Source
java!() { /* proc-macro */ }
Expand description

Re-export the proc macros so users only need to depend on this crate. Compile and run zero-argument Java code at program runtime.

Wraps the provided Java body in a generated class, compiles it with javac, and executes it with java. The return value of static T run() is binary-serialised by the generated main() and deserialised to the inferred Rust type.

Expands to Result<T, inline_java::JavaError>, so callers can propagate errors with ? or surface them with .unwrap().

For run() methods that take parameters, use java_fn! instead.

§Options

Optional key = "value" pairs may appear before the Java body, separated by commas:

  • javac = "<args>" — extra arguments for javac (shell-quoted).
  • java = "<args>" — extra arguments for java (shell-quoted).

§Examples

use inline_java::java;

// Scalar return value
let x: i32 = java! {
    static int run() {
        return 42;
    }
}.unwrap();

// Array return
let primes: Vec<i32> = java! {
    static int[] run() {
        return new int[]{2, 3, 5, 7, 11};
    }
}.unwrap();

// Extra javac flags
let greeting: String = java! {
    javac = "-sourcepath .",
    import com.example.demo.*;
    static String run() {
        return new HelloWorld().greet();
    }
}.unwrap();

// Visibility modifiers are accepted — `public`, `private`, `protected` all work
let v: i32 = java! {
    public static int run() { return 99; }
}.unwrap();