0基础学网站设计,南阳做网站公司,手机的网站建设目标是什么意思,灰色网站这几天需要写一个用到Java模块的程序#xff0c;但是Java是不可能写的#xff0c;这辈子都不可能写的#xff0c;只能搞搞interop了。目前市面上已有的基本上是IKVM.NET和JNBridgePro#xff0c;后者没太了解技术细节#xff0c;前者看起来是只有单向的互操作#xff08;… 这几天需要写一个用到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 --depth1
cd java.interop然后先简单修改一下几个文件。diff --git a/Directory.Build.props b/Directory.Build.props
index 521e68a..1da7d44 100644
--- a/Directory.Build.propsb/Directory.Build.props-43,6 43,8 XamarinAndroidToolsDirectory Condition $(XamarinAndroidToolsDirectory) $(MSBuildThisFileDirectory)external\xamarin-android-tools/XamarinAndroidToolsDirectory/PropertyGroupPropertyGroupJavaCPath/usr/lib/jvm/java-8-openjdk-amd64/bin/javac/JavaCPathJarPath/usr/lib/jvm/java-8-openjdk-amd64/bin/jar/JarPathJavacSourceVersion Condition $(JavacSourceVersion) 1.8/JavacSourceVersionJavacTargetVersion 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.csb/samples/Hello/Program.cs-10,6 10,7 namespace Hellopublic 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.csb/src/Java.Interop/Java.Interop/JniRuntime.cs-149,7 149,8 namespace Java.InteropDebug.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.csb/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和JavacTargetVersion11由于使用的部分代码还是java8标准所以建议继续JavacSourceVersion1.8。暂时还没实验jdk11。文件差不多编辑完了来编译。make src/Java.Runtime.Environment/Java.Runtime.Environment.dll.config
make
mono bin/TestDebug/Hello.exe此时会显示运行成功的样子。如果没成功那就是我忘了哪个步骤没写逃Hello World!
Part 2!
# JniEnvironment.EnvironmentPointer94212541059552
vm.SafeHandle140206052962432
java.lang.Object0x55af91090e50/L
hashcode1735600054
WITHIN: GetCreatedJavaVMs: 140206052962432
POST: GetCreatedJavaVMs: 140206052962432
接下来的文章将大致介绍如何在C#中直接调用Java代码而不是JniType一顿操作。