|
| 1 | +using Java.Interop; |
| 2 | + |
| 3 | +using Mono.Options; |
| 4 | + |
| 5 | +bool showHelp = false; |
| 6 | + |
| 7 | +var jreOptions = new JreRuntimeOptions { |
| 8 | +}; |
| 9 | + |
| 10 | +var options = new OptionSet { |
| 11 | + "Using the JVM from C#!", |
| 12 | + "", |
| 13 | + "Options:", |
| 14 | + { "jvm=", |
| 15 | + $"{{PATH}} to JVM to use.", |
| 16 | + v => jreOptions.JvmLibraryPath = v }, |
| 17 | + { "cp=|classpath", |
| 18 | + $"Add {{JAR-OR-DIRECTORY}} to JVM classpath.", |
| 19 | + v => jreOptions.ClassPath.Add (v)}, |
| 20 | + { "J=", |
| 21 | + $"Pass the specified option to the JVM.", |
| 22 | + v => jreOptions.AddOption (v) }, |
| 23 | + { "h|help", |
| 24 | + "Show this message and exit.", |
| 25 | + v => showHelp = v != null }, |
| 26 | +}; |
| 27 | +options.Parse (args); |
| 28 | + |
| 29 | +if (showHelp) { |
| 30 | + options.WriteOptionDescriptions (Console.Out); |
| 31 | + return; |
| 32 | +} |
| 33 | + |
| 34 | +if (string.IsNullOrEmpty (jreOptions.JvmLibraryPath) || !File.Exists (jreOptions.JvmLibraryPath)) { |
| 35 | + Error ("Option -jvm=PATH is required. PATH is a full path to the JVM native library to use, e.g. `libjli.dylib`."); |
| 36 | + return; |
| 37 | +} |
| 38 | + |
| 39 | +var jre = jreOptions.CreateJreVM (); |
| 40 | + |
| 41 | +// We now have a JVM! |
| 42 | +// The current thread is implicitly attached to the JVM. |
| 43 | +// Access of `JniEnvironment` members on other threads will implicitly attach those threads to the JVM. |
| 44 | + |
| 45 | +// |
| 46 | +// Useful background info: the JNI documentation! https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html |
| 47 | +// |
| 48 | + |
| 49 | +var Object_class = JniEnvironment.Types.FindClass ("java/lang/Object"); |
| 50 | +Console.WriteLine ($"Object_class={Object_class}"); |
| 51 | +var Object_ctor = JniEnvironment.InstanceMethods.GetMethodID (Object_class, "<init>", "()V"); |
| 52 | +var Object_val = JniEnvironment.Object.NewObject (Object_class, Object_ctor); |
| 53 | + |
| 54 | +Console.WriteLine ($"Object_val={Object_val}"); |
| 55 | + |
| 56 | +// Invoke `Object.toString()` |
| 57 | +var Object_toString = JniEnvironment.InstanceMethods.GetMethodID (Object_class, "toString", "()Ljava/lang/String;"); |
| 58 | +unsafe { |
| 59 | + var Object_desc = JniEnvironment.InstanceMethods.CallObjectMethod (Object_val, Object_toString, null); |
| 60 | + Console.WriteLine ($"Object_val.toString()={JniEnvironment.Strings.ToString (Object_desc)}"); |
| 61 | + |
| 62 | + // When JNI returns a `jobject` or `jclass` value, JNI returns a *JNI Object Reference*. |
| 63 | + // The `JniObjectReference` struct is used to store JNI Local, Global, and Weak Global references. |
| 64 | + // |
| 65 | + // When an object reference is no longer required, it should be explicitly deleted. |
| 66 | + |
| 67 | + JniObjectReference.Dispose (ref Object_desc); |
| 68 | +} |
| 69 | + |
| 70 | +JniObjectReference.Dispose (ref Object_class); |
| 71 | +JniObjectReference.Dispose (ref Object_val); |
| 72 | + |
| 73 | +// There are some OO wrappers over the core `JniEnvironment` members. `JniType` is useful. |
| 74 | +var Object_type = new JniType ("java/lang/Object"); |
| 75 | +var Object_ctor2 = Object_type.GetConstructor ("()V"); |
| 76 | + |
| 77 | +unsafe { |
| 78 | + var Object_val2 = Object_type.NewObject (Object_ctor2, null); |
| 79 | + var Object_desc = JniEnvironment.InstanceMethods.CallObjectMethod (Object_val2, Object_toString, null); |
| 80 | + Console.WriteLine ($"Object_val.toString()={JniEnvironment.Strings.ToString (Object_desc)}"); |
| 81 | +} |
| 82 | + |
| 83 | +void Error (string message) |
| 84 | +{ |
| 85 | + var app = Path.GetFileNameWithoutExtension (Environment.GetCommandLineArgs ()[0]); |
| 86 | + Console.Error.WriteLine ($"{app}: {message}"); |
| 87 | +} |
0 commit comments