Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Run Java code from Rust!

License

NotificationsYou must be signed in to change notification settings

benanders/rjni

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This library provides complete FFI bindings to the Java Native Interface, aswell as a safe and intuitive wrapper around most these bindings (lacking arraysupport 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 andreturn values (no support for arrays yet)

Documentation

Documentation can be foundhere.

Usage

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

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

For example, if you have a/path/to/project/com/me/Test.java file (ie. theclasscom.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, andspecify the correct JVM version:

use rjni::{Jvm,Version,Classpath,Options};fnmain(){// Create a custom classpath, pointing to the directory containing the root// of your Java codeletmut 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 classpathletmut options =Options::new();options.version(Version::V16);options.classpath(classpath);// Create the JVM with these optionslet jvm =Jvm::new(options).unwrap();// Get the `com.me.Test` class using the JVMlet class = jvm.class("com/me/Test").unwrap();// ...}

See theexamples folder for more example code on how to call static methodson classes, instantiate objects, call methods on objects, and access objectfields.

About

Run Java code from Rust!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp