Abstract
The real concept driving this article is to develop solutions using the .NET or Java Framework that interoperate with heterogeneous systems or even mutually communicate with each other. The Java Virtual Machine (JVM) exposes the Java Native Interface (JNI) that allows other programs to control the JVM. For instance, load classes, create instances, and run methods. This sample shows the amazing tactics of mingling both C# and Java code in one source code file and producing the desired results. Nowadays, the organization works on multiple projects simultaneously and the corporating infrastructure is usually configured with a couple of platforms such as .NET, Java, and PHP to fulfill the developer's requirements. So, it is good to interoperate or set up communication across these technologies to save human efforts and computer resources because one algorithm developed on the .NET platform could be packed as an API or DLL file and be consumed across diverse platforms.
Essential
The aspirant is supposed to be convenient in both the Java and C# programming languages as well as having a deep understanding of the JVM and CLR internals. This operation requires the subsequent software to be installed as listed below.
- Jni4net
- Jdk
- JVM
- Eclipse (optional)
- .NET Framework
- Visual Studio 2010 (optional)
Interoperability
Interoperability enables communication, data exchange, or program execution among diverse systems in a way that requires the user to have little awareness of the underlying operations of those systems. The programmers can get the advantage of the power of the Java platform by applying the JNI without having to abandon their investments in legacy code because the JNI is a core part of the JVM. Programmers can address interoperability issues once and expect their solution to work with all implementations of the Java platform. Do suspicions often occur in the mind of a developer, such as why is interoperability so important? What kind of issues is it precisely addressing? To what extent is interoperability technology between Java and .NET beneficial? Here, the subsequent advantages are outlined to justify interoperability as in the following.
- Migrations: migration must be well planned and carefully executed when the system is updated or replaced and often involves moving an application a few parts at a time. This way of dividing a system for migration purposes often creates a demand for interoperability because some parts that have been migrated still might need to communicate with others that have not.
- Reusability: most established companies have a number of legacy systems. By the term legacy, I mean technology that's not been keenly developed today. For example, a system located in the data center that's still in production but no longer offers a strategic advantage to the company is a legacy system. A plan to move these systems to a new platform might be a longer-term strategy. A solution that has the ability to interoperate with these systems has the potential to extend the life of these systems and more importantly, the knowledge of the developers who work with them.
- Adoption: when organizations want to deploy new technology such as the .NET Framework, it's rare that they simply replace an entire application or system. In many cases, a replacement is normally triggered by a pilot project. Such a pilot tends to be a short-term project with an intent to prove that the technology can work well with existing systems and applications. The ability of this pilot project to interoperate with existing production systems is imperative and in many cases, can often determine its success.
Java Native Interface (JNI)
Sometimes, a situation arises where Java or .NET alone does not fit the needs of your application. We can write and execute applications entirely in Java and the .NET Framework independently. Programmers use the JNI to write native methods to handle those situations when an application cannot be written in Java and .NET independently. The Java Native Interface framework is devised to enable Java code running in a Java Virtual Machine (JVM) to call and be called by, native applications and libraries written in other programming languages, for instance, .NET, C++, and assembly. JNI can also modify an existing application that is developed and executed under the CLR to be accessible to Java applications. In simple terms, we can say that we can call .NET applications and libraries in Java and vice-versa using the JNI. Native code relatively runs faster than JVM code so such implementations can assist, to a great extent, in solving complicated and time-critical operations.
Java, native languages, and .NET have their own data type infrastructure. During the interoperability or communication of these diverse languages among each other, it is necessary to comply with a common data type scheme so that all participant languages can send messages between each other and follow the data type semantics. The following table describes the primitive types in the Java programming language and the corresponding types in the JNI.

The Java platform is implemented on top of a host platform. So, it is necessary to allow Java applications to work closely with other language's native code written so that the developer can adapt the Java platform to build applications that were traditionally written in .NET and C++. The JNI is specially designed to interoperate or combine Java applications with the CLR or native code. The JNI typically offers two types of native code: native application and library. We can utilize the JNI to write native methods that allow Java applications to call functions implemented in native libraries. Java applications are called native methods in the same way that they call methods implemented in the Java programming language.
Jni4net Framework
Sometimes circumstances demand that Java and .NET technology send messages or communicate with each other in order to save human effort or reduce the programming glitches. It is not necessary for a company infrastructure to choose J2EE or the .NET Framework particularly. An algorithm written in the Java language could be consumed or manipulated in a .NET Framework or vice-versa. Jni4net is a mechanism that allows a Java program to call a function in a C# program and a C# program to call a method in a Java program. The jni4net framework includes both .NET FCL and JDK core classes to possibly use a Reflection technology implementation across the boundary.

So, jni4net could be conceived as an API that is frequently used in Java or .NET technology. Apart from that, this framework offers a couple of other functionalities such as garbage collection, automatic proxy generation, and inter-process communication. The jni4net framework also addresses the interoperability mechanisms similarly supported by other languages. This is, however, not designed for a specific implementation of the Java Virtual Machine. Rather, it is a native interface that can be supported by every implementation of the Java Virtual Machine.









Tarik AlkathiriPosted Dec 1, 2021, 8:18 PM
Thank you very much for the solution. I followed your instruction and every thing is fine except the file jni4net.j-0.8.6.0.jar I could not find it in the downloaded library. could you please show me how to get the jar file.
Ken GeePosted Nov 29, 2017, 10:48 AM
Trying jni4net to gain access to methods and classes in existing JAR. The object gets the correct path, but the only file listed when the command findJAR is the jni4net.jar. What am I doing wrong? what procedure needs to be followed to get other JAR files to load properly?
Guest UserPosted Oct 30, 2014, 6:34 AM
got it Ajay
Sam HobbsPosted Oct 29, 2014, 5:38 PM
Note that unmanaged languages and compliers support mixing languages in a single executable but .Net does not. For example we cannot combine C# and VB.Net in one executable. Therefore the absence of support for mixing C# and Java in a single executable is consistent with the design of .Net. I have written many programs back in the day (before Windows) when the operating systems did not support dynamic linking. I agree it would be good to be able to mix languages in a single executable but Dynamic Link Libraries seem to not be a performance problem and use of separate files for multiple languages seem to be good for most things. So if it is possible to call Java in a separate file from C# then it seems to be a good solution most of the time at least.
Guest UserPosted Oct 29, 2014, 5:40 AM
I mean why we need to leverage Java in c#
Guest UserPosted Oct 29, 2014, 5:40 AM
Hi Ajay... good information, may i know whats the merit for this cross functionality.