2019-11-29 21:39:32 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from os.path import join
|
2020-05-29 09:26:49 +00:00
|
|
|
from jnius.env import get_java_setup
|
2015-05-01 21:57:22 +00:00
|
|
|
|
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
|
|
|
|
|
2013-11-25 02:54:07 +00:00
|
|
|
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
|
2012-08-18 10:59:15 +00:00
|
|
|
JavaVMOption *options
|
|
|
|
ctypedef struct JavaVMOption:
|
|
|
|
char *optionString
|
|
|
|
void *extraInfo
|
2012-08-14 01:42:43 +00:00
|
|
|
|
2013-06-24 15:58:20 +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
|
2012-08-18 10:59:15 +00:00
|
|
|
cdef bytes py_bytes
|
2014-05-09 07:22:56 +00:00
|
|
|
import jnius_config
|
2012-08-18 10:59:15 +00:00
|
|
|
|
2014-05-09 07:22:56 +00:00
|
|
|
optarr = jnius_config.options
|
2014-12-10 18:50:58 +00:00
|
|
|
cp = jnius_config.expand_classpath()
|
2017-10-29 21:28:10 +00:00
|
|
|
optarr.append("-Djava.class.path={0}".format(cp))
|
2014-05-09 07:22:56 +00:00
|
|
|
|
2020-07-10 15:17:41 +00:00
|
|
|
optarr = [str_for_c(x) for x in optarr]
|
2014-05-09 07:22:56 +00:00
|
|
|
options = <JavaVMOption*>malloc(sizeof(JavaVMOption) * len(optarr))
|
|
|
|
for i, opt in enumerate(optarr):
|
|
|
|
options[i].optionString = <bytes>(opt)
|
|
|
|
options[i].extraInfo = NULL
|
2012-08-14 01:42:43 +00:00
|
|
|
|
|
|
|
args.version = JNI_VERSION_1_4
|
2012-08-18 10:59:15 +00:00
|
|
|
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
|
|
|
|
|
2019-11-29 21:39:32 +00:00
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
# uh, let's see if this works and cleanup later
|
2020-05-29 09:26:49 +00:00
|
|
|
java = get_java_setup('win32')
|
|
|
|
jdk_home = java.get_javahome()
|
2019-11-29 21:39:32 +00:00
|
|
|
for suffix in (
|
|
|
|
('bin', 'client'),
|
|
|
|
('bin', 'server'),
|
2022-05-29 08:47:45 +00:00
|
|
|
('bin', 'default'),
|
2019-11-29 21:39:32 +00:00
|
|
|
('jre', 'bin', 'client'),
|
|
|
|
('jre', 'bin', 'server'),
|
2022-05-29 08:47:45 +00:00
|
|
|
('jre', 'bin', 'default'),
|
2019-11-29 21:39:32 +00:00
|
|
|
):
|
|
|
|
path = join(jdk_home, *suffix)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
continue
|
|
|
|
with os.add_dll_directory(path):
|
|
|
|
try:
|
|
|
|
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Exception("Unable to create jni env, no jvm dll found.")
|
|
|
|
|
|
|
|
else:
|
|
|
|
ret = JNI_CreateJavaVM(&jvm, <void **>&_platform_default_env, &args)
|
|
|
|
|
2014-05-09 07:22:56 +00:00
|
|
|
free(options)
|
|
|
|
|
|
|
|
if ret != JNI_OK:
|
|
|
|
raise SystemError("JVM failed to start")
|
|
|
|
|
|
|
|
jnius_config.vm_running = True
|
2020-04-08 13:56:19 +00:00
|
|
|
import traceback
|
|
|
|
jnius_config.vm_started_at = ''.join(traceback.format_stack())
|
2012-08-14 01:42:43 +00:00
|
|
|
|
2014-05-09 07:22:56 +00:00
|
|
|
cdef JNIEnv *get_platform_jnienv() except NULL:
|
2013-06-24 15:58:20 +00:00
|
|
|
if _platform_default_env == NULL:
|
2012-08-14 01:42:43 +00:00
|
|
|
create_jnienv()
|
2013-06-24 15:58:20 +00:00
|
|
|
return _platform_default_env
|