由于错误“在“ Err”值上调用“ Result :: unwrap()”的“主线程”而导致错误,因此无法创建通用JNIEnv

时间:2019-12-18 18:40:21

标签: rust java-native-interface

我想创建一个具有固定值JNIEnv(例如“ fake javastring”)的通用JObject(以及JString)来测试一些需要{ {1}}和JEnv才能正常工作。我不想运行Java VM。

我尝试创建通用的JObjectNone,但是失败并显示以下错误:

JNIEnv

我的代码-Cargo.toml

failures:

---- main_tests::Jstrings::return_fake_string stdout ----
thread 'main_tests::Jstrings::return_fake_string' panicked at 'called `Result::unwrap()` on an `Err` value: Error(NullPtr("from_raw ptr argument"), State { next_error: None, backtrace: InternalBacktrace })', src/libcore/result.rs:999:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:197
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:208
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:474
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:381
   6: rust_begin_unwind
             at src/libstd/panicking.rs:308
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::result::unwrap_failed
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/macros.rs:18
   9: core::result::Result<T,E>::unwrap
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/result.rs:800
  10: rust::empty_jnienv
             at src/main.rs:10
  11: rust::main_tests::Jstrings::return_fake_string
             at src/main.rs:29
  12: rust::main_tests::Jstrings::return_fake_string::{{closure}}
             at src/main.rs:27
  13: core::ops::function::FnOnce::call_once
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/ops/function.rs:231
  14: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/liballoc/boxed.rs:704
  15: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  16: test::run_test::run_test_inner::{{closure}}
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/panicking.rs:272
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/panic.rs:394
             at src/libtest/lib.rs:1468

main.rs

[package]
name = "rust"
version = "0.1.0"
edition = "2018"

[dependencies]
libc = "0.2"
jni = { version = "0.10.2", default-features = false }

backend.rs

mod backend;

extern crate libc;

use jni::objects::{JObject, JString};
use jni::JNIEnv;
use std::ptr;

pub unsafe fn empty_jnienv() -> jni::JNIEnv<'static> {
    jni::JNIEnv::from_raw(ptr::null_mut()).unwrap()
}

pub unsafe fn empty_jobj() -> jni::objects::JObject<'static> {
    jni::objects::JObject::null()
}

fn main() {}

#[cfg(test)]
mod main_tests {
    use super::*;

    mod Jstrings {
        use super::*;

        #[test]
        fn return_fake_string() {
            let this_string = "fake javastring";
            let jenv = unsafe { empty_jnienv() };
            let jobj = unsafe { empty_jobj() };
            let jstr: jni::objects::JString = jenv.new_string(this_string.to_owned()).unwrap();
            assert!(unsafe {
                backend::Java_com_example_android_MainActivity_test(jenv, jobj, jstr) == true
            });
        }
    }
}

是否可以在不运行Java VM的情况下创建use jni::objects::{JObject, JString}; use jni::JNIEnv; use std::ffi::{CStr, CString}; #[no_mangle] pub unsafe extern "C" fn Java_com_example_android_MainActivity_test( env: JNIEnv, _: JObject, j_recipient: JString, ) -> bool { let mut this_return = false; let recipient = CString::from(CStr::from_ptr( env.get_string(j_recipient).unwrap().as_ptr(), )); if recipient.to_str().unwrap() == "fake javastring" { this_return = true; } this_return } JNIEnv的情况下解决此问题?

2 个答案:

答案 0 :(得分:1)

正如JNIEnv::from_raw()的文档所述,它并没有做太多,但是

  

从原始指针创建JNIEnv。

     

仅执行空检查-否则假定指针有效。

因此,传递ptr::null_mut()已经触发了此操作,然后出现错误信息的第一行,提示这一点:

called `Result::unwrap()` on an `Err` value: Error(NullPtr("from_raw ptr argument") [...]

从字面上讲,空指针导致错误,而不是JNIEnv,因此不能unwrap()

TL; DR:不,您要做的事情将无法使用,Java无法在没有Java的情况下工作,并且根据代码的某些后续部分,可能值得补充的是,如果没有Android,Android也将无法工作。即使您正确配置和使用Java,让它运行Activity也将是一个全新的挑战。

答案 1 :(得分:1)

如果您查看jni.h的内部,您会看到JNIEnv*是一个充满函数指针的大结构:

struct JNINativeInterface_ {
    void *reserved0;
    void *reserved1;
    void *reserved2;

    void *reserved3;
    jint (JNICALL *GetVersion)(JNIEnv *env);

    jclass (JNICALL *DefineClass)
      (JNIEnv *env, const char *name, jobject loader, const jbyte *buf,
       jsize len);
    jclass (JNICALL *FindClass)
      (JNIEnv *env, const char *name);

    // and so on
};

typedef const struct JNINativeInterface_ *JNIEnv;

如果您确实想采用这种方法,则可以仅实现代码所需的功能,然后将其分配给JNINativeInterface_的各个成员。 我对您使用的Rust桥了解不多,但是转换这样的JNIEnv指针应该可以。

当然,最后一个问题是:您实际在测试什么