转载

服务器程序的Xamarin-Java.Interop体验(一)

这几天需要写一个用到Java模块的程序,但是Java是不可能写的,这辈子都不可能写的,只能搞搞interop了。

目前市面上已有的基本上是IKVM.NET和JNBridgePro,后者没太了解技术细节,前者看起来是只有单向的互操作(JVM是跑在CLR上的,或者将Java字节码翻译到MSIL)。

想起来之前好像说.NET 5.0要支持Java互操作,但是翻了翻dotnet/runtime库,丝毫看不出来仓库内在搞支持。后来就想了想,换了xamarin/java.interop库研究看看。

按照之前Xamarin.Android的做法的话,互操作应该是双向的。C#这边可以继承Java的类,然后Java那边也会生成访问对应C#代码的代码。

然后发现……他们正在支持.NET Core 3.1,但是其JNI库引用的头文件还是mono的,而且用到了pthread和dlfcn的头文件,也就是说……现在必须在Linux/macOS和mono下运行。

那么先来build一下吧~

此处以Ubuntu 18.04为例。首先需要准备一些系统依赖。编译要很久,还是选择apt安装吧。

sudo apt install gnupg ca-certificates sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list sudo apt update sudo apt install openjdk-8-jdk mono-devel nuget dotnet-sdk-3.1 sudo ln -s /usr/include/mono-2.0/mono /usr/include/mono 

编译的时候TargetFrameworks要用到netcoreapp3.1,所以得安装上。然后就是编译内容了。

先clone一下。

git clone https://github.com/xamarin/java.interop --depth=1 cd java.interop 

然后先简单修改一下几个文件。

diff --git a/Directory.Build.props b/Directory.Build.props index 521e68a..1da7d44 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -43,6 +43,8 @@ <XamarinAndroidToolsDirectory Condition=" '$(XamarinAndroidToolsDirectory)' == '' ">$(MSBuildThisFileDirectory)external/xamarin-android-tools</XamarinAndroidToolsDirectory> </PropertyGroup> <PropertyGroup> + <JavaCPath>/usr/lib/jvm/java-8-openjdk-amd64/bin/javac</JavaCPath> + <JarPath>/usr/lib/jvm/java-8-openjdk-amd64/bin/jar</JarPath> <JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">1.8</JavacSourceVersion> <JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">1.8</JavacTargetVersion> <_BootClassPath Condition=" '$(JreRtJarPath)' != '' ">-bootclasspath "$(JreRtJarPath)"</_BootClassPath> diff --git a/samples/Hello/Program.cs b/samples/Hello/Program.cs index 6ffacbb..9f45fac 100644 --- a/samples/Hello/Program.cs +++ b/samples/Hello/Program.cs @@ -10,6 +10,7 @@ namespace Hello public static unsafe void Main (string[] args) { Console.WriteLine ("Hello World!"); + JreRuntime.Initialize("/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so"); try { var ignore = JniRuntime.CurrentRuntime; } catch (InvalidOperationException e) { diff --git a/src/Java.Interop/Java.Interop/JniRuntime.cs b/src/Java.Interop/Java.Interop/JniRuntime.cs index 6de9021..f9fa0de 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.cs @@ -149,7 +149,8 @@ namespace Java.Interop Debug.Assert (count == 0); var available = GetAvailableInvocationPointers ().FirstOrDefault (); if (available == IntPtr.Zero) - throw new NotSupportedException ("No available Java runtime to attach to. Please create one."); + return null; + //throw new NotSupportedException ("No available Java runtime to attach to. Please create one."); var options = new CreationOptions () { DestroyRuntimeOnDispose = false, InvocationPointer = available, diff --git a/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs b/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs index ea1489f..9ca06b0 100644 --- a/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs +++ b/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs @@ -72,6 +72,14 @@ namespace Java.Interop { public class JreRuntime : JniRuntime { + public static void Initialize(string path) + { + int r = NativeMethods.java_interop_jvm_load (path); + if (r != 0) { + throw new Exception ($"Could not load JVM path `{path}` ({r})!"); + } + } + static int CreateJavaVM (out IntPtr javavm, out IntPtr jnienv, ref JavaVMInitArgs args) { return NativeMethods.java_interop_jvm_create (out javavm, out jnienv, ref args); 

另外,OpenJDK11应该也是可用的,不过得注意JavacSourceVersion和JavacTargetVersion=11,由于使用的部分代码还是java8标准所以建议继续JavacSourceVersion=1.8。暂时还没实验jdk11。

文件差不多编辑完了,来编译。

make src/Java.Runtime.Environment/Java.Runtime.Environment.dll.config make mono bin/TestDebug/Hello.exe 

此时会显示运行成功的样子。如果没成功,那就是我忘了哪个步骤没写(逃)

Hello World! Part 2! # JniEnvironment.EnvironmentPointer=94212541059552 vm.SafeHandle=140206052962432 java.lang.Object=0x55af91090e50/L hashcode=1735600054 WITHIN: GetCreatedJavaVMs: 140206052962432 POST: GetCreatedJavaVMs: 140206052962432 

接下来的文章将大致介绍如何在C#中直接调用Java代码,而不是JniType一顿操作。

原文  http://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654078140&idx=2&sn=615bbe722a454315c6056a41e34f1f81
正文到此结束
Loading...