How can I import JavaVM from the jni crate without

2019-08-22 11:32发布

I am trying to emulate the test cases at https://github.com/jni-rs/jni-rs/blob/master/tests/jni_api.rs and https://github.com/jni-rs/jni-rs/blob/master/tests/util/mod.rs . I have created a project with main.rs

use jni::{InitArgsBuilder, JNIVersion, JavaVM};

fn main() {
    let jvm_args = InitArgsBuilder::new()
        .version(JNIVersion::V8)
        .option("-Xcheck:jni")
        //.option(format!("-Djava.class.path={}", heinous_classpath()))
        .build()
        .unwrap_or_else(|e| panic!("{}", e.display_chain().to_string()));

    let jvm = JavaVM::new(jvm_args);
}

and Cargo.toml:

[package]
name = "rust_call_jni"
version = "0.1.0"
authors = ["Robert Forsman <git@thoth.purplefrog.com>"]
edition = "2018"

[dependencies]
jni = "0.12.3"

When I do a cargo build I get the following error:

error[E0432]: unresolved import `jni::InitArgsBuilder`
 --> src/main.rs:1:11
  |
1 | use jni::{InitArgsBuilder, JNIVersion, JavaVM};
  |           ^^^^^^^^^^^^^^^ no `InitArgsBuilder` in the root

error[E0599]: no function or associated item named `new` found for type `jni::wrapper::java_vm::vm::JavaVM` in the current scope
  --> src/main.rs:12:23
   |
12 |     let jvm = JavaVM::new(jvm_args);
   |               --------^^^
   |               |
   |               function or associated item not found in `jni::wrapper::java_vm::vm::JavaVM`

I'm using Rust 1.34.2.

How can I modify my source code to properly import and invoke the constructors?

标签: rust
1条回答
叼着烟拽天下
2楼-- · 2019-08-22 12:25

Based on the pointer from Svetlin Zarev, I was able to rummage around in the documentation and figure out the proper syntax to enable the invocation feature.

The most important thing was a modification to Cargo.toml. The docstring for the jni crate might benefit from including this clause for folks with less experience writing Cargo.toml:

[package]
name = "rust_call_jni"
version = "0.1.0"
authors = ["Robert Forsman <git@thoth.purplefrog.com>"]
edition = "2018"

[dependencies.jni]
version = "0.12.3"
features = ["invocation"]

I had to modify main.rs to fix some errors that were masked by the earlier errors:

use jni::{InitArgsBuilder, JNIVersion, JavaVM};

fn main() {
    let jvm_args = InitArgsBuilder::new()
        .version(JNIVersion::V8)
        .option("-Xcheck:jni")
        //.option(&format!("-Djava.class.path={}", heinous_classpath()))
        .build()
        .unwrap_or_else(|e| //panic!("{}", e.display_chain().to_string())
        panic!("{:?}", e));

    let jvm = JavaVM::new(jvm_args);
}

It turns out there is another Cargo.toml syntax for dependencies with multiple attributes I discovered later on:

jni = {version="0.12.3", features=["invocation"]}
查看更多
登录 后发表回答