1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::os::raw::c_void;
use std::path::MAIN_SEPARATOR;
use java_locator::{get_jvm_dyn_lib_file_name, locate_jvm_dyn_library};
use jni_sys::{
JavaVM,
jclass,
jint,
JNIEnv,
jsize,
};
use libloading;
use crate::{utils, errors};
use crate::errors::opt_to_res;
type JNIGetCreatedJavaVMs = unsafe extern "system" fn(vmBuf: *mut *mut JavaVM, bufLen: jsize, nVMs: *mut jsize) -> jint;
type JNICreateJavaVM = unsafe extern "system" fn(
pvm: *mut *mut JavaVM,
penv: *mut *mut c_void,
args: *mut c_void,
) -> jint;
lazy_static! {
static ref JVM_LIB: libloading::Library = {
let full_path = format!("{}{}{}",
locate_jvm_dyn_library().expect("Could find the jvm dynamic library"),
MAIN_SEPARATOR,
get_jvm_dyn_lib_file_name()
);
libloading::Library::new(full_path).expect("Could not load the jvm dynamic library")
};
static ref GET_CREATED_JVMS: libloading::Symbol<'static, JNIGetCreatedJavaVMs> = unsafe {
JVM_LIB.get(b"JNI_GetCreatedJavaVMs").expect("Could not find symbol: JNI_GetCreatedJavaVMs")
};
static ref CREATE_JVM: libloading::Symbol<'static, JNICreateJavaVM> = unsafe {
JVM_LIB.get(b"JNI_CreateJavaVM").expect("Could not find symbol: JNI_CreateJavaVM")
};
}
pub(crate) fn get_created_java_vms(vm_buf: &mut Vec<*mut JavaVM>, buf_len: jsize, n_vms: *mut jsize) -> jint {
unsafe {
GET_CREATED_JVMS(vm_buf.as_mut_ptr(), buf_len, n_vms)
}
}
pub(crate) fn create_java_vm(
jvm: *mut *mut JavaVM,
penv: *mut *mut c_void,
args: *mut c_void,
) -> jint {
unsafe {
CREATE_JVM(jvm, penv, args)
}
}
pub(crate) fn find_class(env: *mut JNIEnv, classname: &str) -> errors::Result<jclass> {
unsafe {
let cstr = utils::to_c_string(classname);
let fc = opt_to_res((**env).FindClass)?;
let jc = (fc)(
env,
cstr,
);
utils::drop_c_string(cstr);
Ok(jc)
}
}