Best way to call VB.NET function from Java?

2019-09-01 10:29发布

问题:

I'm interning as a software developer for a company. There is a client-installed software program written in C# that has dll's in VB - both on the .NET framework. Our entire cloud-based server is written in Java, so for now, my task is simply to call a function in Java that gets/sets a private(? sorry, not really familiar with VB so I might be using this term inappropriately) variable located within the dll. Ultimately, though, my task will be to collect data from the client program and put it in our server. I've been researching how to go about doing this for the past few days and have found a few options:

  • Java Native Access (JNA)
  • Java Native Interface (JNI)
  • COM/ActiveX bridges (though I can't entirely say for sure that I know what these are/how to use them_
  • JNBridge

Now as far as I know, these are the issues I've found with each, respectively:

  • Using this option requires an intermediary from Java to .NET using either C or C++, and given that I only really know Java, I'd not really like to get tangled in this mess.
  • Seems a tad bit more complicated than JNA, and I think it has no real advantages over JNA anyways. They both appear to have the need of a C/C++ intermediary.
  • ... can't really say much aside from the fact that I'm not really sure how I can go about implementing this method.
  • This and other similar bridges has to be a last resort. The developer license for this product is quite pricey, and I'm sure they we don't have the resource to use this product. I could try asking, but I don't really think it's in the place of an intern to ask that a company invest so much.

To add to the fact that I'm not really that adept in programming, I've been left to mostly do this on my own so thanks so much in advance for reading my question. If there's something I haven't fully explained that necessitates further explanation, please let me know! Any other tips/pointers would be immensely appreciated.

回答1:

You can expose VB.NET as a webservice to be consumed in Java...



回答2:

I would never go with webservices in this case. COM/ActiveX is also to be skipped as soon as posible to avoid further issues ;) JNA/JNI sounds good but there are a lot of hidden things on your way. Proposed JNBridge is good idea as it gives you possibility to focus on your core issue and going with this approach you just hand over all the interoperability (JNA/JNI) tasks to third-party.

As alternative option I would propose you to check light counterpart of JNBridge called Javonet. It does not require proxy generation, or installation of any plugins. You get just one JAR file which opens you access to any custom .NET DLL and .NET Framework out-of-the-box without any changes on .NET side.

The performance is very high almost native and possbilities cover 90% of cases like: using any .NET object, methods, fields, arrays, generics, events, exceptions, data types translation and others...

In addition the price/value index of such bridges is so high that it will never pay off to start custom development until you have only one property to read or one method to execute. Otherwise I would go with third-party bridge.

Javonet works with any .NET DLL including VB.NET, C# etc.. Check this short sample code of using .NET from JAVA using Javonet syntax:

NObject yourObj = Javonet.New("Your.Namespace.YourDotNetClass");
yourObj.invoke("YourMethod","arg1",2);

As you see from Javonet API on JAVA side you get new object called "NObject" it is type of handle to any .NET object on which you can perform operations like invoking methods, getting/settings fields and arguments you can provide as JAVA type String, Integer, Float and they will be automatically translated to .NET types.

For more details check the quick start guide: http://www.javonet.com/#getting_started



回答3:

I can recommand JNA together with UnmanagedExports since it requires only C#, VB.NET or F# .dll with the following Header

C#:

[RGiesecke.DllExport.DllExport]
public static String yourFunction(String yourParameter)
{
    return "CSharp String";
}

after that you can consume it with JNA like any other C or C++ .dll in Java. You can find an example and some restrictions I couldn't come around in my answer at this similar question: Call DLL from Java using JNA

I only tried it with C#, but as stated in the docu it should work with C#, F# and VB.NET.