timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
163 stars 44 forks source link

Fix support for Windows #10

Closed timob closed 5 years ago

timob commented 7 years ago

The current master doesn't support Windows anymore because the functions:

jni_GetDefaultJavaVMInitArgs
jni_CreateJavaVM

are not implemented in windows.go. Please see linux.go for how these are implemented. Basically this file dynamically loads the jvm library.

timob commented 7 years ago

windows.go needs to load the dll and find the two functions.

discoli-zz commented 6 years ago

@timob any update on this issue? really looking forward to using this!

hanxueming126 commented 5 years ago

in winows.go,you can add code like following:

package jnigi

/*
#cgo CFLAGS:-I'D:/develop tool/jdk1.7.0_80/include' -I'D:/develop tool/jdk1.7.0_80/include/win32'
#cgo LDFLAGS:-ljvm -L'D:/develop tool/jdk1.7.0_80/jre/bin/server'
#include<jni.h>
#include<stdio.h>
#include<stdlib.h>
#include<Windows.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) {
    jint ret =  var_JNI_CreateJavaVM(pvm, penv, args);
    return ret;

}
char* dllPath = "D:/develop tool/jdk1.7.0_80/jre/bin/server/jvm.dll";
type_JNI_GetDefaultJavaVMInitArgs Get_JNI_GetDefaultJavaVMInitArgs(){
     HMODULE jvm_dll = LoadLibrary(dllPath);
     type_JNI_GetDefaultJavaVMInitArgs ret =(type_JNI_GetDefaultJavaVMInitArgs) GetProcAddress(jvm_dll,"JNI_GetDefaultJavaVMInitArgs");
     return ret;
}

type_JNI_CreateJavaVM Get_JNI_CreateJavaVM(){
     HMODULE jvm_dll = LoadLibrary(dllPath);
     type_JNI_CreateJavaVM ret =(type_JNI_CreateJavaVM) GetProcAddress(jvm_dll,"JNI_CreateJavaVM");
     return ret;
}

void InitJNI(){
    var_JNI_GetDefaultJavaVMInitArgs = Get_JNI_GetDefaultJavaVMInitArgs();
    var_JNI_CreateJavaVM = Get_JNI_CreateJavaVM();
}

*/
import "C"

import(
    "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(){
    C.InitJNI()
}

And when using the function,add args -Xint when inital jvm args

timob commented 5 years ago

@hanxueming126 thanks for that code. I've got a change coming that does what you have but mostly in Go init() function like the other OS files.

timob commented 5 years ago

Should be fixed in https://github.com/timob/jnigi/commit/4a6d12459ba7cd175426a185b96cb357338cfe3b