最好的选择.在Java应用程序中使用.Net Dll [英] Best choice. Use .Net Dll in Java Application

查看:116
本文介绍了最好的选择.在Java应用程序中使用.Net Dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道三个是关于此的一些问题,但我没有找到确切的答案. 我们有一个.Net dll(C#),没有任何依赖关系或p/Invoke.因此,它是一个完整的.net平台库. 我们的一位客户希望在Java应用程序中使用它.

什么是最佳选择? 我一直在寻找jni4net wich可能是一个完美的解决方案,但似乎不支持.Net中的泛型(我们的dll使用了很多泛型词典和集合)

这是JNA的最佳选择吗?

预先感谢

解决方案

几年前,我遇到了类似的问题.我有一个用Delphi编写的dll. (Delphi是Borland出售的基于Pascal的Windows应用程序开发工具.)我需要从Java调用dll,但是某些dll函数具有与Java不兼容的参数和返回类型. (作为一个有趣的,甚至无关紧要的东西,为微软发明C#的安德斯·海斯伯格(Anders Hejlsberg)也为Borland发明了Delphi.

1)我确实使用jni来允许我的Java代码调用dll.

2)我在Delphi中编写了一个瘦包装程序dll,这是我的Java jni代码调用的实际dll.对于与Java完全兼容的那些函数,包装dll只是充当传递函数,直接调用实际的dll函数并返回返回值.对于那些不兼容的功能,包装dll定义了与Java兼容的相应方法,并在调用实际的dll之前做了从Java到Delphi的适当转换.

3)我还在jni调用上方编写了一个瘦包装器对象.再一次,在大多数情况下,Java包装器直接对那些与Java和Delphi完全兼容的函数进行了jni调用.但是,就我而言,一些函数需要传递Delphi对象.因此,我要做的是定义相应的Java对象.我的Java包装对象的主要目的是获取这些Java对象,将它们转换为与我的Delphi包装dll兼容的参数,然后进行适当的jni调用.另外,对于那些传回对象的dll函数,我的Java包装器从jni调用中获取了与Java兼容的返回值,并创建并组装了适当的对象.

这听起来可能需要做很多工作,但实际上并非如此(我的dll有100多种方法,以及十几种Delphi对象类型).完成后,编写实际上使用dll的Java应用程序代码非常简单.

关于泛型,这可能是个问题.但是,如果在现实生活中,您支持的对象类型的数量相对较少(并且通常是这样),则可以在包装器中为每种对象类型编写单独的调用. (这就是我们那些记得Java 2在发明泛型之前就曾经做过的事情的人,即使它不那么优雅,它也能正常工作.)您的应用程序Java代码可能会使用泛型.包装程序将根据传入的实际类型进行适当的调用.

希望这会给您一些有关如何进行的想法.

I know three is some questions about this, but I've not found the exact response. We have a .Net dll (C#) with no dependencies or p/Invoke. So it's a full .net platform library. One of our clientes wants to use it in a Java Application.

What's the best choice? I've been looking at jni4net wich could be a perfect solution, but it seems don't support generics in .Net (our dll uses lot of generics dictionaries and collections)

It's JNA the best choice?

Thanks in advance

解决方案

I had a similar problem several years ago. I had a dll written in Delphi. (Delphi was a Pascal-based Windows app development tool sold by Borland.) I needed to call the dll from Java, but some of the dll functionss had parameters and return types that that were incompatible with Java. (As an interesting, if irrelevant, aside, Anders Hejlsberg, who invented C# for Microsoft, also invented Delphi for Borland.) Here is how I solved the problem.

1) I did use jni to allow my Java code to call a dll.

2) I wrote a thin wrapper dll in Delphi that was the actual dll called by my Java jni code. For those functions that were completely compatible with Java, the wrapper dll simply acted as a pass though, directly calling the actual dll functions and returning the return value. For those functions that were not compatible, the wrapper dll defined corresponding methods that were compatible with Java and did the appropriate translation from Java to Delphi before calling the actual dll.

3) I also wrote a thin wrapper object above my jni calls. Again, for the most part, the java wrapper directly made jni calls for those functions that were completely compatible between Java and Delphi. However, in my case, a few functions required that I pass in Delphi objects. So, what I did was define corresponding Java objects. The main purpose of my Java wrapper object was to take these Java objects, translate them into parameters that were compatible with my Delphi wrapper dll, and then make the apprropriate jni call. Also, for those dll functions which passed back an object, my java wrapper took the java-compatible return value from the jni call, and created and asssembled the appropriate object.

This may sound like a lot of work, but it really was not (and my dll had over 100 methods, and a dozen or so Delphi object types). When I was done, writing the Java application code that actually used the dll was very straight forward.

Regarding the generics, that could be a problem. But, if in real life, the number of object types you support is relatively small (and it often is), you can just write separate calls for each object type in your wrapper. (That is what those of us who remember Java 2 used to do before they invented generics and it worked just fine, even if it was a bit less elegant.) Your application Java code could stll use generics; the wrapper would make the appropriate call based on the actual type that was passed in.

Hopefully this will give you some ideas on how you might proceed.

这篇关于最好的选择.在Java应用程序中使用.Net Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆