public void LoadVM(Dictionary <string, string> options) { string currentDir = Directory.GetCurrentDirectory(); string jvmDll = Path.Combine("jre", "bin", "server", "jvm.dll"); if ((!File.Exists(jvmDll))) { throw new Exception("Error determining the location of the Java Runtime Environment"); } // Set the directory to the location of the JVM.dll. // This will ensure that the API call JNI_CreateJavaVM will work Directory.SetCurrentDirectory(Path.GetDirectoryName(jvmDll)); var args = new JavaVMInitArgs(); args.version = JNIVersion.JNI_VERSION_10; args.ignoreUnrecognized = JavaVM.BooleanToByte(true); // True if (options.Count > 0) { args.nOptions = options.Count; var opt = new JavaVMOption[options.Count]; int i = 0; foreach (KeyValuePair <string, string> kvp in options) { opt[i++].optionString = Marshal.StringToHGlobalAnsi(kvp.Key.ToString() + "=" + kvp.Value.ToString()); } fixed(JavaVMOption *a = &opt[0]) { // prevents the garbage collector from relocating the opt variable as this is used in unmanaged code that the gc does not know about args.options = a; } } var result = JavaVM.JNI_CreateJavaVM(out IntPtr javaVirtualMachine, out IntPtr environment, &args); if (result != JNIReturnValue.JNI_OK) { throw new Exception("Cannot create JVM " + result.ToString()); } jvm = new JavaVM(javaVirtualMachine); env = new JNIEnv(environment); Directory.SetCurrentDirectory(currentDir); }
public void LoadVM(Dictionary <string, string> options, bool AddToExistingJVM) { // Get the location of the current version of the JVM.dll string jreVersion = (string)Registry.GetValue(JRE_REGISTRY_KEY, "CurrentVersion", null); string keyName = Path.Combine(JRE_REGISTRY_KEY, jreVersion); string jvmDir = (string)Registry.GetValue(keyName, "RuntimeLib", null); if ((jvmDir.Length == 0) || (!File.Exists(jvmDir))) { throw new Exception("Error determining the location of the Java Runtime Environment"); } // Set the directory to the location of the JVM.dll. // This will ensure that the API call JNI_CreateJavaVM will work Directory.SetCurrentDirectory(Path.GetDirectoryName(jvmDir)); var args = new JavaVMInitArgs(); switch (Convert.ToInt32((decimal.Parse(jreVersion.Substring(0, 3)) - 1) / 2 * 10)) { case 0: throw new Exception("Unsupported java version. Please upgrade your version of the JRE."); case 1: args.version = JNIVersion.JNI_VERSION_1_2; break; case 2: args.version = JNIVersion.JNI_VERSION_1_4; break; default: args.version = JNIVersion.JNI_VERSION_1_6; break; } args.ignoreUnrecognized = JavaVM.BooleanToByte(true); // True if (options.Count > 0) { args.nOptions = options.Count; var opt = new JavaVMOption[options.Count]; int i = 0; foreach (KeyValuePair <string, string> kvp in options) { opt[i++].optionString = Marshal.StringToHGlobalAnsi(kvp.Key.ToString() + "=" + kvp.Value.ToString()); } fixed(JavaVMOption *a = &opt[0]) { // prevents the garbage collector from relocating the opt variable as this is used in unmanaged code that the gc does not know about args.options = a; } } if (!AttachToCurrentJVMThread) { IntPtr environment; IntPtr javaVirtualMachine; int result = JavaVM.JNI_CreateJavaVM(out javaVirtualMachine, out environment, &args); if (result != JNIReturnValue.JNI_OK) { throw new Exception("Cannot create JVM " + result.ToString()); } jvm = new JavaVM(javaVirtualMachine); env = new JNIEnv(environment); } else { AttachToCurrentJVM(args); } }