Run Java code from Rust!


Java Native Interface Bindings for Rust

This library provides complete FFI bindings to the Java Native Interface, as well as a safe and intuitive wrapper around most these bindings (lacking array support for now).

Features include:

  • Creating and configuring an instance of a Java Virtual Machine
  • Loading classes
  • Calling static methods on classes
  • Setting and retrieving public static fields on classes
  • Instantiating objects from a class
  • Calling methods on objects
  • Setting and retrieving public fields on objects
  • Using all primitive Java types and other Java objects as arguments and return values (no support for arrays yet)


Documentation can be found here.


First you'll need to compile your Java source code, either as separate .class files, or package them together as a .jar archive.

You need to make sure you target the Java compiler to the JVM version you plan to use. This is done through the -target and -source command line arguments to javac.

For example, if you have a /path/to/project/com/me/ file (ie. the class and you intend to target the 1.6 JVM:

$ javac -target 1.6 -source 1.6 /path/to/project/com/me/

This will create a /path/to/project/com/me/Test.class file.

Then when you create the JVM in Rust, you need to add /path/to/project (ie. the directory containing the root of your Java code) to the classpath, and specify the correct JVM version:

use rjni::{Jvm, Version, Classpath, Options};

fn main() {
	// Create a custom classpath, pointing to the directory containing the root
	// of your Java code
	let mut classpath = Classpath::new();

	// Create a series of configuration options for the JVM, specifying the
	// version of the JVM we want to use (1.6), and our custom classpath
	let mut options = Options::new();

	// Create the JVM with these options
	let jvm = Jvm::new(options).unwrap();

	// Get the `` class using the JVM
	let class = jvm.class("com/me/Test").unwrap();

	// ...

See the examples folder for more example code on how to call static methods on classes, instantiate objects, call methods on objects, and access object fields.

You might also like...
Twiggy🌱 is a code size profiler
Twiggy🌱 is a code size profiler

Twiggy 🌱 A code size profiler for Wasm Guide | Contributing | Chat Built with 🦀 🕸 by The Rust and WebAssembly Working Group About Twiggy is a code

This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents.

lua-to-ts This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents. Code that fails to be

A simple code for checking crate 'prost' on WebAssembly (🦀 + 🕸️ = 💖)

rust-wasm-prost This repository is a simple code for checking crate 'prost' on WebAssembly ( 🦀 + 🕸️ = 💖 ). What is prost? prost is a Protocol Buffe

Slitter is a C- and Rust-callable slab allocator implemented primarily in Rust, with some C for performance or to avoid unstable Rust features.

Slitter is a less footgunny slab allocator Slitter is a classically structured thread-caching slab allocator that's meant to help write reliable long-

A Rust crate for automatically generating C header files from Rust source file.

Please be aware that this crate is no longer actively maintained, please look into the much more feature rich cbindgen instead. rusty-cheddar rusty-ch

Rust-ffi-guide - A guide for doing FFI using Rust

Using unsafe for Fun and Profit A guide to traversing the FFI boundary between Rust and other languages. A rendered version is available here. This gu

Automatically generates Rust FFI bindings to C (and some C++) libraries.

bindgen bindgen automatically generates Rust FFI bindings to C (and some C++) libraries. For example, given the C header doggo.h: typedef struct Doggo

Safe interop between Rust and C++

CXX — safe FFI between Rust and C++ This library provides a safe mechanism for calling C++ code from Rust and Rust code from C++, not subject to the m

Safe Rust bridge for creating Erlang NIF functions

Rustler Documentation | Getting Started | Example Rustler is a library for writing Erlang NIFs in safe Rust code. That means there should be no ways t

  • How to build from source?

    How to build from source?

    It's unclear how to build from source at the moment. Attempting to build from a Windows 10 box for the stable MSVC target with no attempt at special configuration yields:

    ~/…/experiments/rjni master$ cargo build --example instance
        Updating registry ``
       Compiling libc v0.2.40
       Compiling rjni v0.0.1 (file:///…/experiments/rjni)
    error: linking with `link.exe` failed: exit code: 1181
      = note: // snip, horribly long linker invocation
      = note: LINK : fatal error LNK1181: cannot open input file 'jvm.lib'

    I understand that this project is a work-in-progress, and this may not be an immediate priority for you. However, adding documentation like this will enable people to experiment and contribute, if it's something you think might help this project along. :)

    opened by ErichDonGubler 7
  • Publish at

    Publish at

    All the existing solutions there help accessing Rust from Java, while it is useful to have a vice-versa library (for instance for getting the system settings which are accessible from Java API only).

    opened by snuk182 0
  • JNI reference handling

    JNI reference handling

    All references produced implicitly by the JVM are local refs. They need to be managed with PushLocalFrame and PopLocalFrame. Furthermore, any reference that is meant to be shared cross-thread must be a global ref or weak global ref.

    opened by DemiMarie 0
Ben Anderson
I'm a student at the University of Melbourne studying Statistics
Ben Anderson
Embedding Rust in Java

Java/Rust Example An example project showing how to call into Rust code from Java. OSX Linux Windows Requirements Java 7+ Rust (tested with 1.0, night

drrb 314 Oct 6, 2022
eJNI is a Rust crate to make working with Java's JNI less painful by providing abstractions.

eJNI provides abstractions for often-used classes from the Java standard library, like Map and List. Besides this eJNI also provides easy ways to work with Java's primitives and their object counterparts (e.g int and Integer).

Tobias de Bruijn 11 Feb 13, 2022
Java for Rust

j4rs j4rs stands for 'Java for Rust' and allows effortless calls to Java code from Rust and vice-versa. Features Rust to Java direction support (call

aston 347 Nov 20, 2022
Rust bindings to the Java Native Interface — JNI

JNI Bindings for Rust This project provides complete JNI bindings for Rust, allowing to: Implement native Java methods for JVM and Android in Rust Cal

null 752 Nov 28, 2022
Rust library for build scripts to compile C/C++ code into a Rust library

A library to compile C/C++/assembly into a Rust library/application.

Alex Crichton 1.3k Nov 28, 2022
A project for generating C bindings from Rust code

cbindgen   Read the full user docs here! cbindgen creates C/C++11 headers for Rust libraries which expose a public C API. While you could do this by h

Ryan Hunt 1.6k Dec 1, 2022
A Web-App written in Rust with Yew, using the same SyntaxHighlighter from Google Code Archive as

PlanetB SyntaxHighlighter About This is a small app, providing static files to have a frontend to format your code so you can paste it with styles to

Christof Weickhardt 2 Aug 8, 2022
A collection of unsound rust functions using entirly *safe* code

A collection of unsound rust functions using entirly *safe* code

null 2 Sep 6, 2022
Turns running Rust code into a serializable data structure.

WasmBox WasmBox turns running Rust code into a serializable data structure. It does this by compiling it to WebAssembly and running it in a sandbox. T

drifting in space 11 Oct 17, 2022
An interactive scripting language where you can read and modify code comments as if they were regular strings

An interactive scripting language where you can read and modify code comments as if they were regular strings. Add and view text-based visualizations and debugging information inside your source code file.

Sumeet Agarwal 13 Apr 28, 2022