Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
j4rs
j4rs stands for 'Java for Rust' and allows effortless calls to Java code, from Rust.
Usage
Basics
use ;
// Create a JVM
let jvm = new_jvm.unwrap;
// Create a java.lang.String instance
let string_instance = jvm.create_instance.unwrap;
// The instances returned from invocations and instantiations can be viewed as pointers to Java Objects.
// They can be used for further Java calls.
// For example, the following invokes the `isEmpty` method of the created java.lang.String instance
let boolean_instance = jvm.invoke.unwrap;
// If we need to transform an `Instance` to Rust value, the `to_rust` should be called
let rust_boolean: bool = jvm.to_rust.unwrap;
println!;
// The above prints:
// The isEmpty() method of the java.lang.String instance returned true
// Static invocation
let _static_invocation_result = jvm.invoke_static.unwrap;
Callback support
j4rs
provides the means for Java to Rust callbacks.
In the Java world, a Class that can do Native Callbacks must extend the class
org.astonbitecode.j4rs.api.invocation.NativeCallbackSupport
For example, consider the following Java class.
The performCallback
method spawns a new Thread and invokes the doCallback
method in this Thread. The doCallback
method is inherited by the NativeCallbackSupport
class.
;
;
In the Rust world, the asynchronous invocation result will come from Java via a function that should be defined. This function should be of type fn(Jvm, Instance) -> ()
We can asynchronously invoke the performCallback
method by calling the invoke_async
of the Jvm
.
// Asynchronous invocation of a method of a Java instance. The invocation result will come from Java via a callback
// Create an Instance of a class that supports Native Callbacks
// (the class just needs to extend the `org.astonbitecode.j4rs.api.invocation.NativeCallbackSupport`)
let i = jvm.create_instance
.unwrap;
// Invoke asynchronously the method
let _ = jvm.invoke_async;
// Wait a little bit in order to see the callback
// We should see the following in the console:
// Asynchronously got from Java: THIS IS FROM CALLBACK!
let ten_millis = from_millis;
sleep;
Passing arguments from Rust to Java
j4rs uses the InvocationArg
enum to pass arguments to the Java world.
Users can benefit of the existing From
implementations for several basic types:
let i1 = from; // Creates an arg of java.lang.String
let my_string = "a string".to_owned;
let i2 = from; // Creates an arg of java.lang.String
let i3 = from; // Creates an arg of java.lang.Boolean
let i4 = from; // Creates an arg of java.lang.Byte
let i5 = from; // Creates an arg of java.lang.Character
let i6 = from; // Creates an arg of java.lang.Short
let i7 = from; // Creates an arg of java.lang.Long
let i8 = from; // Creates an arg of java.lang.Float
let i9 = from; // Creates an arg of java.lang.Double
And for Vec
s:
let my_vec: = vec!;
let i10 = from;
The Instance
s returned by j4rs can be transformed to InvocationArg
s and be used for invoking methods as well:
let one_more_string_instance = jvm.create_instance.unwrap;
let i11 = from;
Casting
An Instance
may be casted to some other Class:
let instantiation_args = vec!;
let instance = jvm.create_instance.unwrap;
jvm.cast.unwrap;
Adding jars in the classpath
If we have one jar that needs to be accessed using j4rs
, we need to add it in the classpath during the JVM creation:
let classpath_entry = new;
let jvm = new_jvm.unwrap;
j4rs Java library
The jar for j4rs
is available in the Maven Central. It may be used by adding the following dependency in a pom:
io.github.astonbitecode
j4rs
0.1.1
provided
Note that the scope
is provided
. This is because the j4rs
Java resources are always available with the j4rs
crate.
Use like this in order to avoid possible classloading errors.
Next?
- Implement macros to facilitate
j4rs
users
Possibly something like:
// Instantiation
let i12 = jnew!;
// Invocation
let i13 = j!;
- Fix sharing and using the created
Jvm
s in different Rust threads.
Licence
At your option, under:
- Apache License, Version 2.0, (http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (http://opensource.org/licenses/MIT)