diff --git a/.github/workflows/create.yml b/.github/workflows/create.yml index 6176e36..5becc33 100644 --- a/.github/workflows/create.yml +++ b/.github/workflows/create.yml @@ -22,8 +22,6 @@ jobs: # exclude problematic combinations exclude: - - os: windows-latest - python: '3.8' - os: windows-latest python: '2.7' - os: macOs-latest diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 0306ef8..39bf140 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -7,7 +7,7 @@ jobs: matrix: python: - '2.7' - # - '3.6' + - '3.6' - '3.7' - '3.8' java: @@ -26,8 +26,6 @@ jobs: # exclude problematic combinations exclude: - - os: windows-latest - python: '3.8' - os: windows-latest python: '2.7' - os: macOs-latest diff --git a/jnius/__init__.py b/jnius/__init__.py index 154868b..a00e309 100644 --- a/jnius/__init__.py +++ b/jnius/__init__.py @@ -9,9 +9,38 @@ All the documentation is available at: http://pyjnius.readthedocs.org __version__ = '1.2.1.dev3' -from .env import get_jnius_lib_location -from .jnius import * # noqa -from .reflect import * # noqa +from .env import get_jnius_lib_location, get_jdk_home + +import os +import sys +if sys.platform == 'win32' and sys.version_info >= (3, 8): + path = os.path.dirname(__file__) + jdk_home = get_jdk_home(sys.platform) + with os.add_dll_directory(path): + for suffix in ( + ('bin', 'client'), + ('bin', 'server'), + ('jre', 'bin', 'client'), + ('jre', 'bin', 'server'), + ): + path = os.path.join(jdk_home, *suffix) + if not os.path.isdir(path): + continue + + with os.add_dll_directory(path): + try: + from .jnius import * # noqa + from .reflect import * # noqa + except Exception as e: + pass + else: + break + else: + raise Exception("Unable to create jni env, no jvm dll found.") +else: + from .jnius import * # noqa + from .reflect import * # noqa + from six import with_metaclass # XXX monkey patch methods that cannot be in cython. diff --git a/jnius/env.py b/jnius/env.py index f9e7d18..824ee8d 100644 --- a/jnius/env.py +++ b/jnius/env.py @@ -10,6 +10,7 @@ from platform import machine from subprocess import Popen, check_output, PIPE from shlex import split + PY2 = sys.version_info.major < 3 machine = machine() # not expected to change at runtime @@ -140,6 +141,7 @@ def get_jdk_home(platform): return jdk_home + def get_osx_framework(): framework = Popen( '/usr/libexec/java_home', diff --git a/jnius/jnius_jvm_desktop.pxi b/jnius/jnius_jvm_desktop.pxi index 717db80..1979a8b 100644 --- a/jnius/jnius_jvm_desktop.pxi +++ b/jnius/jnius_jvm_desktop.pxi @@ -1,3 +1,7 @@ +import sys +import os +from os.path import join +from jnius.env import get_jdk_home from cpython.version cimport PY_MAJOR_VERSION # on desktop, we need to create an env :) @@ -43,7 +47,31 @@ cdef void create_jnienv() except *: args.nOptions = len(optarr) args.ignoreUnrecognized = JNI_FALSE - ret = JNI_CreateJavaVM(&jvm, &_platform_default_env, &args) + if sys.version_info >= (3, 8): + # uh, let's see if this works and cleanup later + jdk_home = get_jdk_home('win32') + for suffix in ( + ('bin', 'client'), + ('bin', 'server'), + ('jre', 'bin', 'client'), + ('jre', 'bin', 'server'), + ): + path = join(jdk_home, *suffix) + if not os.path.isdir(path): + continue + with os.add_dll_directory(path): + try: + ret = JNI_CreateJavaVM(&jvm, &_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, &_platform_default_env, &args) + free(options) if ret != JNI_OK: diff --git a/jnius/jnius_jvm_dlopen.pxi b/jnius/jnius_jvm_dlopen.pxi index f6e4f86..34c6be4 100644 --- a/jnius/jnius_jvm_dlopen.pxi +++ b/jnius/jnius_jvm_dlopen.pxi @@ -2,7 +2,7 @@ include "config.pxi" import os from shlex import split from subprocess import check_output -from os.path import dirname +from os.path import dirname, join from os import readlink from sys import platform from .env import get_jnius_lib_location @@ -64,6 +64,7 @@ cdef void create_jnienv() except *: cdef JavaVMOption *options cdef int ret cdef bytes py_bytes + cdef void *handle import jnius_config JAVA_HOME = os.getenv('JAVA_HOME') or find_java_home() @@ -80,7 +81,8 @@ cdef void create_jnienv() except *: ELSE: lib_path = str_for_c(os.path.join(JAVA_HOME, JNIUS_LIB_SUFFIX)) - cdef void *handle = dlopen(lib_path, RTLD_NOW | RTLD_GLOBAL) + handle = dlopen(lib_path, RTLD_NOW | RTLD_GLOBAL) + if handle == NULL: raise SystemError("Error calling dlopen({0}: {1}".format(lib_path, dlerror()))