|
| 1 | +usingJava.Interop; |
| 2 | + |
| 3 | +usingMono.Options; |
| 4 | + |
| 5 | +boolshowHelp=false; |
| 6 | + |
| 7 | +varjreOptions=newJreRuntimeOptions{ |
| 8 | +}; |
| 9 | + |
| 10 | +varoptions=newOptionSet{ |
| 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 | +varjre=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 | +varObject_class=JniEnvironment.Types.FindClass("java/lang/Object"); |
| 50 | +Console.WriteLine($"Object_class={Object_class}"); |
| 51 | +varObject_ctor=JniEnvironment.InstanceMethods.GetMethodID(Object_class,"<init>","()V"); |
| 52 | +varObject_val=JniEnvironment.Object.NewObject(Object_class,Object_ctor); |
| 53 | + |
| 54 | +Console.WriteLine($"Object_val={Object_val}"); |
| 55 | + |
| 56 | +// Invoke `Object.toString()` |
| 57 | +varObject_toString=JniEnvironment.InstanceMethods.GetMethodID(Object_class,"toString","()Ljava/lang/String;"); |
| 58 | +unsafe { |
| 59 | +varObject_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(refObject_desc); |
| 68 | +} |
| 69 | + |
| 70 | +JniObjectReference.Dispose(refObject_class); |
| 71 | +JniObjectReference.Dispose(refObject_val); |
| 72 | + |
| 73 | +// There are some OO wrappers over the core `JniEnvironment` members. `JniType` is useful. |
| 74 | +varObject_type=newJniType("java/lang/Object"); |
| 75 | +varObject_ctor2=Object_type.GetConstructor("()V"); |
| 76 | + |
| 77 | +unsafe { |
| 78 | +varObject_val2=Object_type.NewObject(Object_ctor2,null); |
| 79 | +varObject_desc=JniEnvironment.InstanceMethods.CallObjectMethod(Object_val2,Object_toString,null); |
| 80 | +Console.WriteLine($"Object_val.toString()={JniEnvironment.Strings.ToString(Object_desc)}"); |
| 81 | +} |
| 82 | + |
| 83 | +voidError(stringmessage) |
| 84 | +{ |
| 85 | +varapp=Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]); |
| 86 | +Console.Error.WriteLine($"{app}: {message}"); |
| 87 | +} |
0 commit comments