pyjnius/jnius/jnius_jvm_desktop.pxi

57 lines
1.6 KiB
Cython
Raw Normal View History

from cpython.version cimport PY_MAJOR_VERSION
2012-08-14 01:42:43 +00:00
# on desktop, we need to create an env :)
# example taken from http://www.inonit.com/cygwin/jni/invocationApi/c.html
cdef extern jint __stdcall JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args)
2012-08-14 01:42:43 +00:00
cdef extern from "jni.h":
int JNI_VERSION_1_4
2014-05-09 07:22:56 +00:00
int JNI_OK
2012-08-14 01:42:43 +00:00
jboolean JNI_FALSE
ctypedef struct JavaVMInitArgs:
jint version
jint nOptions
jboolean ignoreUnrecognized
JavaVMOption *options
ctypedef struct JavaVMOption:
char *optionString
void *extraInfo
2012-08-14 01:42:43 +00:00
cdef JNIEnv *_platform_default_env = NULL
2012-08-14 01:42:43 +00:00
2014-05-09 07:22:56 +00:00
cdef void create_jnienv() except *:
2012-08-14 01:42:43 +00:00
cdef JavaVM* jvm
cdef JavaVMInitArgs args
2014-05-09 07:22:56 +00:00
cdef JavaVMOption *options
cdef int ret
cdef bytes py_bytes
2014-05-09 07:22:56 +00:00
import jnius_config
2014-05-09 07:22:56 +00:00
optarr = jnius_config.options
optarr.append("-Djava.class.path=" + jnius_config.expand_classpath())
options = <JavaVMOption*>malloc(sizeof(JavaVMOption) * len(optarr))
for i, opt in enumerate(optarr):
if PY_MAJOR_VERSION >= 3:
opt = opt.encode('utf-8')
2014-05-09 07:22:56 +00:00
options[i].optionString = <bytes>(opt)
options[i].extraInfo = NULL
2012-08-14 01:42:43 +00:00
args.version = JNI_VERSION_1_4
args.options = options
2014-05-09 07:22:56 +00:00
args.nOptions = len(optarr)
2012-08-14 01:42:43 +00:00
args.ignoreUnrecognized = JNI_FALSE
2014-05-09 07:22:56 +00:00
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
free(options)
if ret != JNI_OK:
raise SystemError("JVM failed to start")
jnius_config.vm_running = True
2012-08-14 01:42:43 +00:00
2014-05-09 07:22:56 +00:00
cdef JNIEnv *get_platform_jnienv() except NULL:
if _platform_default_env == NULL:
2012-08-14 01:42:43 +00:00
create_jnienv()
return _platform_default_env