Need example for jni invocation api


#1

I want to call java code from rust and use jni crate for that. There is no documentation or examples how to use JavaVM struct. Could someone provide?

Tried to figure it by myself but with no luck:

code
extern crate jni;

use jni::*;

fn main() {
    let jvm_args = InitArgsBuilder::new()
        .option("-cp").option("src/example.jar").option("Main")
        .build().unwrap();

    JavaVM::new(jvm_args).unwrap();
}
cargo run with backtrace
RUST_BACKTRACE=1 LD_LIBRARY_PATH=lib/jdk1.8.0_162/jre/lib/amd64/server cargo run --release
   Compiling j v0.1.0 (file:///home/onyn/projects/rust-j)
    Finished release [optimized] target(s) in 1.21 secs
     Running `target/release/j`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Other(-3), State { next_error: None, backtrace: Some(stack backtrace:
   0:     0x55b35cdb170c - backtrace::backtrace::trace::h126b02226c1b8f63
   1:     0x55b35cdaffc2 - backtrace::capture::Backtrace::new::h13f44638b2f75119
   2:     0x55b35cdafa8c - error_chain::make_backtrace::h07e455bdcc93d1dd
   3:     0x55b35cdafb24 - <error_chain::State as core::default::Default>::default::h6b3e9c81a5d0c74c
   4:     0x55b35cdaee58 - jni::wrapper::java_vm::vm::JavaVM::new::h3d8c1cfd09a82998
   5:     0x55b35cdada68 - cube::main::h8ecfc43851c65056
   6:     0x55b35cdad8f2 - std::rt::lang_start::{{closure}}::h2c3e9518bc3f4e4e
   7:     0x55b35cdc8b57 - std::rt::lang_start_internal::{{closure}}::ha293dd0b9fbcae9f
                        at libstd/rt.rs:59
                         - std::panicking::try::do_call::hd12baa704e255980
                        at libstd/panicking.rs:479
   8:     0x55b35cdd1e5e - __rust_maybe_catch_panic
                        at libpanic_unwind/lib.rs:102
   9:     0x55b35cdc9303 - std::panicking::try::h8f80ce4b4c3953ca
                        at libstd/panicking.rs:458
                         - std::panic::catch_unwind::h0bf512424ce1df1e
                        at libstd/panic.rs:358
                         - std::rt::lang_start_internal::h764d480f090a00ea
                        at libstd/rt.rs:58
  10:     0x55b35cdadb53 - main
  11:     0x7f3c78b2182f - __libc_start_main
  12:     0x55b35cdad6e8 - _start
  13:                0x0 - <unknown>) })', libcore/result.rs:945:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at libstd/sys_common/backtrace.rs:59
             at libstd/panicking.rs:380
   3: std::panicking::default_hook
             at libstd/panicking.rs:396
   4: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:576
   5: std::panicking::begin_panic
             at libstd/panicking.rs:537
   6: std::panicking::begin_panic_fmt
             at libstd/panicking.rs:521
   7: rust_begin_unwind
             at libstd/panicking.rs:497
   8: core::panicking::panic_fmt
             at libcore/panicking.rs:71
   9: core::result::unwrap_failed
  10: cube::main
  11: std::rt::lang_start::{{closure}}
  12: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:479
  13: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  14: std::rt::lang_start_internal
             at libstd/panicking.rs:458
             at libstd/panic.rs:358
             at libstd/rt.rs:58
  15: main
  16: __libc_start_main
  17: _start