Example #1
0
 def start(self, *jvmoptions, **jvmargs):
     ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True)
     try:
         pjvm = jni.obj(jni.POINTER(jni.JavaVM))
         penv = jni.obj(jni.POINTER(jni.JNIEnv))
         jvm_args = jni.obj(jni.JavaVMInitArgs)
         jvm_args.version = JVM.JNI_VERSION
         jvm_args.nOptions = len(jvmoptions)
         jvm_args.options = joptions = jni.new_array(
             jni.JavaVMOption, jvm_args.nOptions)
         _keep = []
         for i, option in enumerate(jvmoptions):
             optionString = jni.new_cstr(option if isinstance(
                 option, bytes) else str(option).encode("utf-8"))
             _keep.append(optionString)
             jvm_args.options[i].optionString = optionString
             jvm_args.options[i].extraInfo = jni.NULL
         jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE
         err = self._JNI.CreateJavaVM(pjvm, penv, jvm_args)
         del _keep, joptions, jvm_args
         if err != jni.JNI_OK or jni.isNULL(pjvm):
             raise jni.JNIException(
                 err if err != jni.JNI_OK else jni.JNI_ERR,
                 info="JNI_CreateJavaVM")
         self._jnijvm = jni.JVM(pjvm)
         return self._jnijvm, jni.JEnv(penv)
     except Exception as exc:
         try:
             self.handleException(exc)
         finally:
             self._jnijvm = None
Example #2
0
 def __init__(self, jenv: Optional[jni.JNIEnv] = None):
     if jenv is None:
         from .jconstants import EStatusCode
         from .jvm        import JVMException
         raise JVMException(EStatusCode.EDETACHED,
                            "Unable to use JVM: thread detached from the VM")
     pjvm = jni.obj(jni.POINTER(jni.JavaVM))
     jenv.GetJavaVM(pjvm)
     from .jvm import _JVM
     jvm = _JVM()
     jvm.jnijvm = jni.JVM(pjvm)
     self.jvm  = jvm   # jvm._JVM
     self.jenv = jenv  # jni.JNIEnv
Example #3
0
    def start(self, *jvmoptions, **jvmargs):  # -> Tuple['_JVM', jni.JNIEnv]:

        jvmoptions = tuple([
            "-Djava.class.path=" + os.pathsep.join([
                item.partition("=")[2] for item in jvmoptions
                if item.lstrip().startswith("-Djava.class.path=")
            ] + [str(path) for path in INTERNAL_CLASSPATHS])
        ] + [
            item for item in jvmoptions
            if not item.lstrip().startswith("-Djava.class.path=")
        ])
        ignoreUnrecognized = jvmargs.get("ignoreUnrecognized", True)

        try:
            pjvm = jni.obj(jni.POINTER(jni.JavaVM))
            penv = jni.obj(jni.POINTER(jni.JNIEnv))
            jvm_args = jni.obj(jni.JavaVMInitArgs)
            jvm_args.version = JVM.JNI_VERSION
            jvm_args.nOptions = len(jvmoptions)
            jvm_args.options = joptions = jni.new_array(
                jni.JavaVMOption, jvm_args.nOptions)
            _keep = []
            for i, option in enumerate(jvmoptions):
                optionString = jni.new_cstr(option if isinstance(
                    option, bytes) else str(option).encode("utf-8"))
                _keep.append(optionString)
                jvm_args.options[i].optionString = optionString
                jvm_args.options[i].extraInfo = jni.NULL
            jvm_args.ignoreUnrecognized = jni.JNI_TRUE if ignoreUnrecognized else jni.JNI_FALSE
            err = self._jvm.JNI.CreateJavaVM(pjvm, penv, jvm_args)
            del _keep, joptions, jvm_args
            if err != jni.JNI_OK or jni.isNULL(pjvm):
                raise jni.JNIException(
                    err if err != jni.JNI_OK else jni.JNI_ERR,
                    info="JNI_CreateJavaVM")
            self._jvm.jnijvm = jni.JVM(pjvm)
            jenv = jni.JEnv(penv)
            try:
                self._jvm._initialize(jenv)
            except Exception as exc:
                try:
                    self._jvm.jnijvm.DestroyJavaVM()
                except Exception:
                    pass
                raise exc
            return self._jvm, jenv
        except Exception as exc:
            try:
                self.handleException(exc)
            finally:
                self._jvm.jnijvm = None