Java .NET互操作性

Java .NET Interoperability
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我正在开发一个要在Parallel Plesk Panel(Windows)上托管的.NET网站,并且我有一些用Java编写的类,这些类是我想在其网站中使用的swing和JavaMail API编写的.我有两个选择-jni4net和IKVM.net.只是想知道在性能和开发方面,高效便捷的方法是哪一种?

速聊1:
别.将您的Java内容重写为.NET,这样您就可以在一个经过良好测试的平台上运行.
速聊2:
谢谢@ThorbjørnRavnAndersen.您对在这种情况下使用Web服务有何看法?
速聊3:
如果您有一个Java程序,使用Web服务与您的.NET后端进行对话,那很好.从您的问题中我了解到您想在.NET应用程序中使用Java代码.
速聊4:
如果您愿意考虑使用商业软件产品,除了免费的开放源代码软件,您可能还需要查看JNBridge(www.jnbridge.com).[披露:我和JNBridge在一起.]
解决过程1

jni4net的体积更小(二进制大小为1.5MB),并且具有更好的占用空间-但由于上一次存储库更新是在2013年11月执行的,因此开发可能会暂停并且该项目已被放弃BTW,jni4net和LGPL一直处于Alpha阶段,因此不成熟,复杂,需要做更多的工作才能使用它,所以如果您要在商业产品中使用它,将是一个非常大的问题.

IKVM.net(AFAIK)是用CLR编码的Java Runtime几乎全部实现(因为它实现的是OpenJDK,而不是Orcale Java,因此有点破损)平台,并且JIT本身会将Java字节码编译为IL,然后执行它,这类似于QEMU,但是还有另一个问题,我们遇到了三层,第一层是Java Runtime,然后来到CLR,最后是本机代码,因此它非常非常慢,并且另一个问题是初始时间问题.

例如,我编写了一个Java程序,该程序只打印出著名的"Hello World"引号然后退出,然后我要测试,IKVM.net和Orcale JVM(简称JVM)可以完成多少毫秒(我在KVM Windows环境中同时运行两个平台,并具有2个通用3.4 GHz CPU和2G DDR3内存的内核),结果是,JVM在692ms内完成,内存使用量为9MB,而IKVM.net,可能需要大约4923ms的时间和19MB的内存,才能打印出很酷的"Hello World!仅报价!如果我将Java类编译为预编译的IL(ikvmc),则可以将时间减少到大约1907ms,并且内存使用情况与JIT相同.

相比之下,对于JVM和IKVM.net(JIT),JVM的领先时间是其竞争对手的7倍,在内存方面是其2倍,而对于JVM和IKVM.net(预编译),JVM仍然领先对于竞争对手,它的内存是它的2.7倍和1.11倍,而对于IKVM.net(JIT)和IKVM.net(预编译),这将使您的时间翻倍,并且内存占用没有差异,尽管它们仍然比JVM慢和胖.

另外还有一个问题,即预编译的IKVM.net存根必须随处携带整个IKVM.net二进制文件,并且程序包的大小大约增加41MB,然后,我不知道是否可以剥离一些未使用的库,但是在大多数情况下,我们不会这么做.

对于jni4net,从CLR到JVM,打印"Hello World"的时间大约为1567ms,从JVM到CLR呢?有点奇怪,它用了2106毫秒,我不知道为什么差别如此之大,也许是因为Java从头开始动态创建CLR实例,而CLR可以直接以静态方式P / Invoke JNI.

总而言之,jni4net更有可能是CLR和JVM之间代理-它没有实现完整的Java环境,也没有模拟完整的CLR,而是将通过现有资源创建JVM和CLR实例.在IKVM.net上,它更像是伪装成.NetJVM,但是您可以将Java代码注入CLR,尽管您无法在Java端进行.Net的编码,但速度很慢,但不再需要JVM,您可以只安装.Net Framework并享受其余的(但您只能运行Java SE).

为了回答您的问题,根本没有一个好的解决方案-jni4net和IKVM.net实际上根本是两个完全不同的东西,但是如果您想得出一个结论,我想使用IKVM.net-它仍然活跃,强大,可扩展,但又胖又慢.

速聊1:
编写有意义的基准测试怎么样?您的工作主要是评估库的加载和JIT时间,这仅发生一次,并且与大多数用例无关.
速聊2:
只要我知道LPGL并没有那么严格.但是,感谢您的良好解释.

I am developing a .NET website to be hosted on the Parallel Plesk Panel (Windows) and I have some classes written in Java using its swing and JavaMail APIs which I want to use in my website. I have two options - jni4net and IKVM.net. Just want to know which one is the efficient and convenient approach in terms of both performance and effort to develop?

Talk1:
Don't. Rewrite your Java stuff into .NET so you run on a single, well-tested platform.
Talk2:
Thanks @ThorbjørnRavnAndersen . What is your opinion on using Web Services in such scenario?
Talk3:
If you have a Java-program which talks to your .NET backend ONLY using web services that would be fine. I understood from your question you wanted to use the Java code inside your .NET application.
Talk4:
If you are willing to consider commercial software products, in addition to free open-source software, you might want to look at JNBridge (www.jnbridge.com). [Disclosure: I am with JNBridge.]
Solutions1

jni4net is way more smaller(1.5MB of binary size), and have a better footprint -- but the development are likely halted and the project has been abandoned, as the last repository update was executed at the November of 2013, but it was still holding in Alpha stage, thus it's immature, complicated, need to do more work in order to use it, BTW, jni4net was using LGPL, so it will be a very problem if you wanted to use is in your commercial products.

IKVM.net, AFAIK, was a almost-total implementation of Java Runtime coded in CLR (because it implements OpenJDK, not Orcale Java, so it's about a little bit broken) platform, and itself would JIT compiles the Java-bytecode into IL, and then execute it, this is akin to QEMU, but there's another problem, that we have came across three layer, the first layer is Java Runtime, then we came to CLR, and finally native code, thus it's very, very slow, and another problem is initial time issue.

For instance, I have wrote a Java program, that would only print the famous "Hello World" quote then quit, then I would like to test, that how many milliseconds that both IKVM.net and Orcale JVM(shorten to JVM) could do (I'm running both platform in a KVM windows environment with 2 cores of generic 3.4 GHz CPU and 2G of DDR3 memory), and my result is, JVM was finished in 692ms, with the memory usage of 9MB, while IKVM.net, could use about 4923ms, with 19MB of memory footprint, to print a cool "Hello World!" quote only! If I compiled the Java Class into pre-compiled IL(ikvmc), could strip down the amount of time to about 1907ms, and the memory usage is as same as JIT does.

In comparison, for JVM and IKVM.net(JIT), JVM has leaded for about 7x times in time and 2x times in memory against the rival, and for JVM and IKVM.net(pre-compiled), JVM has still leaded for about 2.7x times and 1.11x times in memory against the rival, while for IKVM.net(JIT) and IKVM.net(pre-compiled), it will gives you a boost of double in time and no difference in memory footprint, though they are still slower and fatter than the JVM.

Also there's another problem, that pre-compiled IKVM.net stub must carry the whole IKVM.net binary everywhere, and that was about 41MB increment in the size of a package then, I don't know if I could strip off some unused library, but in most of the case, we won't do that.

For jni4net, the amount of time used to print a "Hello World" is about 1567ms, from CLR to JVM, and what about from JVM to CLR? It's kinda strange, that it used 2106ms, I don't know why the difference are huge, maybe it was because Java is creating a CLR instance from scratch dynamically, while CLR could directly P/Invoke the JNI in statically.

To summarize, the jni4net is more likely to be the proxy between CLR and JVM -- it's not implementing a full Java environment, nor simulating a full CLR, instead it will create both JVM and CLR instance by existing resources. While IKVM.net, it's more like a JVM coded in .Net in disguise, but you could inject Java code into CLR, while you cannot code .Net in Java side, it's slow, but it doesn't require a JVM no more, you could just install the .Net Framework and enjoy the rest(but you could only run the Java SE).

To response to your question, there is no good solution at all -- jni4net and IKVM.net, were actually two totally different thing at all, but if you wanted to force a conclusion, I'd like to use IKVM.net -- it's still active, powerful, extendible, yet fat and slow.

Talk1:
How about writing a meaningful benchmark? Yours is mostly measuring the library loading and JIT time, which happens only once and is irrelevant to most use cases.
Talk2:
As long as I know LPGL is not that restrictive. But thanks for the good explanation.
转载于:https://stackoverflow.com/questions/17378573/java-net-interoperability

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入