Example #1
0
        /// <summary>
        /// Loads Java Environment and Virtual Machine.
        /// </summary>
        /// <param name="path"> On Windows: Path to jvm.dll file. </param>
        /// <param name="parameters"> Optional arguments for virtual machine. </param>
        /// <param name="jniVersion"> Correct JNI version. See: <see cref="JNIConstants"/>. </param>
        /// <param name="attachToExistingJVM"></param>
        public void Load(string path, IDictionary <string, string> parameters = null, int jniVersion = JNIConstants.JNI_VERSION_1_8, bool attachToExistingJVM = false)
        {
            if (string.IsNullOrEmpty(path) ||
                !File.Exists(path))
            {
                throw new ArgumentException("You must specify the correct location of JVM library.", "path");
            }

            // Set the working directory to directory with JVM library.
            Directory.SetCurrentDirectory(Path.GetDirectoryName(path));

            var jvmInitArgs = new JavaVMInitArgs
            {
                version            = jniVersion,
                ignoreUnrecognized = JavaConverter.ToByte(true)
            };

            if (parameters != null &&
                parameters.Count > 0)
            {
                jvmInitArgs.nOptions = parameters.Count;
                var options = new List <JavaVMOption>();

                foreach (var kvp in parameters)
                {
                    options.Add(new JavaVMOption
                    {
                        optionString = Marshal.StringToHGlobalAnsi($"{ kvp.Key }={ kvp.Value }")
                    });
                }

                JavaVMOption[] parsedOptions = options.ToArray();
                fixed(JavaVMOption *a = &parsedOptions[0])
                {
                    jvmInitArgs.options = a;
                }
            }

            if (!attachToExistingJVM)
            {
                IntPtr environment;
                IntPtr javaVM;

                int result = JNINativeImports.JNI_CreateJavaVM(out javaVM, out environment, &jvmInitArgs);
                if (result != JNIConstants.JNI_OK)
                {
                    throw new InvalidOperationException($"Error when creating Java Virtual Machine. [JNI_CreateJavaVM -> Return code: { result }].");
                }

                this.JavaEnvironment = new JavaEnvironment(environment);
                this.JavaEnvironment.VirtualMachine = new JavaVM(javaVM);
            }
            else
            {
                this.AttachToExistingJVM(jvmInitArgs);
            }

            this.Gateway = new GatewayManager(this);
        }
Example #2
0
        public IntPtr DetachCurrentThread()
        {
            if (detachCurrentThread == null)
            {
                JavaConverter.GetDelegateForFunctionPointer(this.InvokeInterface.DetachCurrentThread, ref detachCurrentThread);
            }
            var ret = detachCurrentThread.Invoke(this.NativePointer);

            return(ret);
        }
Example #3
0
        internal IntPtr AttachCurrentThread(out IntPtr penv, JavaVMInitArgs *args)
        {
            if (attachCurrentThread == null)
            {
                JavaConverter.GetDelegateForFunctionPointer(this.InvokeInterface.AttachCurrentThread, ref attachCurrentThread);
            }
            var ret = attachCurrentThread.Invoke(this.NativePointer, out penv, args);

            return(ret);
        }
Example #4
0
        public IntPtr DestroyJavaVM()
        {
            if (destroyJavaVM == null)
            {
                JavaConverter.GetDelegateForFunctionPointer(this.InvokeInterface.DestroyJavaVM, ref destroyJavaVM);
            }
            var ret = destroyJavaVM.Invoke(this.NativePointer);

            return(ret);
        }
Example #5
0
        public IntPtr GetEnv(out IntPtr penv, IntPtr version)
        {
            if (getEnv == null)
            {
                JavaConverter.GetDelegateForFunctionPointer(this.InvokeInterface.GetEnv, ref getEnv);
            }
            var ret = getEnv.Invoke(this.NativePointer, out penv, version);

            return(ret);
        }