Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java .NET Interoperability

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?

like image 944
Nageshwar Saini Avatar asked Jun 29 '13 09:06

Nageshwar Saini


People also ask

Can you use Java and .NET together?

Yes, you can use both the frameworks without any problem as they never affect each other, so just install and start your development.

Does .NET Framework support interoperability?

. NET enables interoperability with unmanaged code through platform invoke services, the System. Runtime. InteropServices namespace, C++ interoperability, and COM interoperability (COM interop).

What is .NET interoperability?

Language interoperability is the ability of code to interact with code that is written using a different programming language. Language interoperability can help maximize code reuse and, therefore, improve the efficiency of the development process.

Can .NET use a Java library?

IKVM.NET is an open source implementation of Java for Mono /Microsoft . NET Framework and makes it possible both to develop . NET applications in Java, and to use existing Java API's and libraries in applications written in any . NET language.


1 Answers

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.

like image 163
Steve Fan Avatar answered Oct 05 '22 05:10

Steve Fan