Skip to main content

java_fn

Macro java_fn 

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

Re-export the proc macros so users only need to depend on this crate. Return a typed Rust function that compiles and runs Java at program runtime.

Like java!, but supports parameters. The parameters declared in the Java run(P1 p1, P2 p2, ...) method become the Rust function’s parameters. Arguments are serialised by Rust and piped to the Java process via stdin; Java reads them with DataInputStream.

Expands to a function value of type fn(P1, P2, ...) -> Result<T, JavaError>. Call it immediately or store it in a variable.

§Supported parameter types

Java typeRust type
bytei8
shorti16
inti32
longi64
floatf32
doublef64
booleanbool
charchar
String&str
Optional<BoxedT>Option<T>
Optional<String>Option<&str>

§Options

Same javac = "..." / java = "..." key-value pairs as java!.

§Examples

use inline_java::java_fn;

// Single int parameter
let double_it = java_fn! {
    static int run(int n) {
        return n * 2;
    }
};
let result: i32 = double_it(21).unwrap();
assert_eq!(result, 42);

// Multiple parameters including String
let greet = java_fn! {
    static String run(String greeting, String target) {
        return greeting + ", " + target + "!";
    }
};
let msg: String = greet("Hello", "World").unwrap();
assert_eq!(msg, "Hello, World!");