Using .NET libraries from JAVA is that possible?
Recent popularization of Web Services seems to solve almost any interoperability issue between JAVA and .NET systems due to platform independence and universal transport channel. And that is true, the flexibility and universality of Web Services based solutions in combination with well-defined standards makes this technology a winner in almost 80% maybe even 90% of cases. But really there are no cons?
Of course there are and cases unresolvable with web service pop out from time to time in many critical projects. The main reason why Web Services does not fit in each case is usually performance, maintenance and development costs. The decision how to solve these issues usually relies on system architects and it could be one of the most important decision in project entailing a lot of different consequences.
How to identify if your case should be solved with web services?
If you are the one to decide you should consider at least following points. Let’s say we have .NET library with some legacy business logic or critical encryption/trading algorithm or .NET only API to corporate CRM, here is how we could analyse this:
- Check if your requirement is worth building dedicated client-server infrastructure. Will you need to run cross machine executions? Remember that web service is client server. In our case we would need to build web service on .NET side and wrap selected methods in our WCF SVC or ASMX interface. If it’s only one method and dll located on the same machine as our JAVA application is it worth to host web server and consume it on local host? Definitely not.
- Check if you can afford it. Each web service call has quite big overhead on each request message if we use xml protocol each simple method is wrapped in xml envelope and passed in http request consuming web server resources, and including all other delays of transport channel, serialization/deserialization and web request processing in fact if it is trading algorithm that places transactions on forex or stock market we should avoid it… same in all other cases were we make a lot of requests in short time or we just count on speed we should not add that much of buzz into our communication.
- Check if it’s not too simple. If the library that we use contains single method for encrypting or calculating something, should we make: web service, client, host web server, allow http communication, build .NET web project just to load one method from one dll and call it once in lifetime of our JAVA project? No.
- Check if it’s not too complicated. Sometimes the amount of code that we want to reuse in JAVA could be oppositely too big. Wrapping thousands of methods in web service and passing thousands of execution places via web service client could cost us a lot in development and further maintenance. Simple example could be the case were we want to build JAVA application with WPF interface, I can’t imagine exposing all WPF framework classes via Web Services implementing call-backs and building out of it responsive UI.
We could assume that so far we have decided if our solution should and could be implemented with web services interoperability approach, but what should we do if it does not fit in there? Most of developers usually do not meet such problems but therefore if they do it’s hard to find a good solution. It is not that easy to get JAVA and .NET work together by our own, without spending for it more time than whole our project is planned for. And of course we should remember that it is not a good practice to reinvent the wheel.
What are the alternatives to use .NET libraries from JAVA application?
Most of us does not realize that there are some native JAVA to .NET bridges available on the market whereas these solutions does exactly this what web service does not so they fill our gap of 20% cases perfectly.
Native JAVA to .NET Bridge leverages JAVA native methods invocation capabilities with native platform specific communication channels and direct invocations on .NET objects. In this way we get .NET process being hosted natively for our JAVA application giving us access to any .NET library directly from JAVA code like they were native JAVA objects. Because of direct invocation passing between processes we get instant execution without delays, overhead and additional infrastructure.
Moreover we get several additional benefits, these are: efficient call-backs mechanism so we can easily subscribe cross platform events, access to any .NET library without any wrappers and implementation on .NET side, lifecycle of objects and processes bound into one runtime and no client server solutions therefore no additional side products to maintain and monitor.
JAVA to .NET interop market is not big. There are only few vendors who managed to deliver efficient and flexible enough solutions that could be reliable for projects of any size.
Choosing right bridge should be driven by simplicity of usage, performance and flexibility. This year there was new solution released called Javonet. It seems to be very attractive due to several reasons. The project was initially born as academic research for object database systems at Polish-Japanese Institute of Information Technology. From very beginnig it was aimed on high performance.
During commercialization it was enhanced with very programmer-friendly API and many useful features. Javonet gives access to any .NET library nevertheless it is custom-made, part of .NET framework or delivered by third party companies. There are no changes in .NET code required and libraries can be loaded from local directories or GAC. And it supports both x86 and x64.
Having access to any library instantly we can just reference them from our JAVA code and work on objects and classes from such library like they were native JAVA objects. Javonet API exposes special NObject variable for JAVA which works as handle to .NET object. Because all invocations and references are done in runtime we can initialize and utilize any object, method, property or field using reflection-style syntax by providing names in constant strings.
JAVA to .NET bridge like Javonet handles internally garbage collector actions and exceptions so we can catch .NET exceptions on JAVA side and if our NObject handle is collected on JAVA side corresponding object gets disposed on .NET side as well. All these features makes such bridging solution a huge cutting edge technology that delivers us all-in-one framework for using .NET code from JAVA.
The possibilities of these solutions are worth exploring as high performance and simple usage leads to new ideas of how we could apply this in our projects. In tutorial and samples we can read step by step guides how to make full JAVA application with WinForms or WPFinterface or how to extend .NET class in JAVA, sometimes even for highly experienced developers these kind of solutions looks like magic.
I highly encourage all JAVA developers to discover and test these solutions to keep in mind new alternatives how CLR and JVM interoperability issues could be resolved in modern world.