forked from timob/jnigi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.go
76 lines (58 loc) · 1.94 KB
/
linux.go
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
// Copyright 2016 Tim O'Brien. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux
package jnigi
/*
#cgo LDFLAGS:-ldl
#include <dlfcn.h>
#include <jni.h>
typedef jint (*type_JNI_GetDefaultJavaVMInitArgs)(void*);
type_JNI_GetDefaultJavaVMInitArgs var_JNI_GetDefaultJavaVMInitArgs;
jint dyn_JNI_GetDefaultJavaVMInitArgs(void *args) {
return var_JNI_GetDefaultJavaVMInitArgs(args);
}
typedef jint (*type_JNI_CreateJavaVM)(JavaVM**, void**, void*);
type_JNI_CreateJavaVM var_JNI_CreateJavaVM;
jint dyn_JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args) {
return var_JNI_CreateJavaVM(pvm, penv, args);
}
*/
import "C"
import (
"os"
"path"
"unsafe"
)
func jni_GetDefaultJavaVMInitArgs(args unsafe.Pointer) jint {
return jint(C.dyn_JNI_GetDefaultJavaVMInitArgs((unsafe.Pointer)(args)))
}
func jni_CreateJavaVM(pvm unsafe.Pointer, penv unsafe.Pointer, args unsafe.Pointer) jint {
return jint(C.dyn_JNI_CreateJavaVM((**C.JavaVM)(pvm), (*unsafe.Pointer)(penv), (unsafe.Pointer)(args)))
}
func init() {
jhPath := "/usr/lib/jvm/default-java"
if val, ok := os.LookupEnv("JAVA_HOME"); ok {
jhPath = val
}
cs := cString(path.Join(jhPath, "jre/lib/amd64/server/libjvm.so"))
defer free(cs)
libHandle := uintptr(C.dlopen((*C.char)(cs), C.RTLD_NOW|C.RTLD_GLOBAL))
if libHandle == 0 {
panic("could not dyanmically load libjvm.so")
}
cs2 := cString("JNI_GetDefaultJavaVMInitArgs")
defer free(cs2)
ptr := C.dlsym(unsafe.Pointer(libHandle), (*C.char)(cs2))
if ptr == nil {
panic("could not find JNI_GetDefaultJavaVMInitArgs in libjvm.so")
}
C.var_JNI_GetDefaultJavaVMInitArgs = C.type_JNI_GetDefaultJavaVMInitArgs(ptr)
cs3 := cString("JNI_CreateJavaVM")
defer free(cs3)
ptr = C.dlsym(unsafe.Pointer(libHandle), (*C.char)(cs3))
if ptr == nil {
panic("could not find JNI_CreateJavaVM in libjvm.so")
}
C.var_JNI_CreateJavaVM = C.type_JNI_CreateJavaVM(ptr)
}