Run Java code from Rust!

Overview

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

Documentation can be found here.

Usage

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/Test.java file (ie. the class com.me.Test) and you intend to target the 1.6 JVM:

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

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();
	classpath.add(&Path::new("/path/to/project"));

	// 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();
	options.version(Version::V16);
	options.classpath(classpath);

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

	// Get the `com.me.Test` 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...
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

Rust solutions to problems in Advent of Code '22.

Advent of Code '22 This repository contains my solutions for AoC'22 problems, written in the language that will save us from cpp. Running the code The

Code for my workshop
Code for my workshop "Production-ready WebAssembly with Rust" presented at RustLab 2023 in Florence

Workshop: Production-ready WebAssembly with Rust A workshop on Rust for WebAssembly by Alberto Schiabel (@jkomyno). 🤓 This workshop was first present

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

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.

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

Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.
Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.

🌼 Zinnia Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.

zzhack-cli is a Command Tool to help you quickly generate a WASM WebApp with simple configuration and zero code
zzhack-cli is a Command Tool to help you quickly generate a WASM WebApp with simple configuration and zero code

English | 中文文档 zzhack-cli is a Command Tool that can help you quickly generate a WASM WebApp with simple configuration and zero code. It's worth menti

Comments
  • 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 `https://github.com/rust-lang/crates.io-index`
       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 crates.io

    Publish at crates.io

    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
Owner
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 318 Jan 1, 2023
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 355 Dec 28, 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 768 Dec 30, 2022
🚀 An OSS project to develop and run serverless applications on WebAssembly

Wasm Workers Server Wasm Workers Server (wws) is a framework to develop and run serverless applications server in WebAssembly. These applications are

VMware  Labs 300 Apr 26, 2023
👾 Run WebAssembly (WASM-4) games on small devices (like PyBadge)

?? gamgee Run WebAssembly (WASM-4) games on small devices. Gamgee is a WASM-4 games emulator written in Rust and designed to be executed on devices wi

Orsinium Labs 5 Feb 27, 2024
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 Dec 21, 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.7k Jan 3, 2023
A Web-App written in Rust with Yew, using the same SyntaxHighlighter from Google Code Archive as planetb.ca

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 Dec 14, 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