How to use .Net dll in Java

2019-03-22 05:03发布

I have dll created in vb.net. How can i use its functions in JAVA.

I found something JNI while searching on google , but not getting it. Is there any simple documentation with example.

3条回答
可以哭但决不认输i
2楼-- · 2019-03-22 05:33

I would recommend Java Native Access (JNA) as its easier than using JNI. Lets say you have a DLL with some functions,

  • Create an java interface which has the same method signatures as the functions in DLL.

For example

public interface NativeExample{

  public int method1(String param1);
  public boolean mehthod2();

}
  • Now following is the way you load the DLL (assuming its name is NativeLib.dll)

    NativeExample nativeExample= (NativeExample) Native.loadLibrary("NativeLib", NativeExample.class);

Once you have this, you can call the method from the DLL via java methods.

`nativeExample.method("test");`

`nativeExample.method2();`

For mappings of the datatypes between Java and Native, please refer the the link above.

Here is one more example.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-22 05:36

Well, there are third party tools / libraries that will help you connect Java to .NET. If you want to do it yourself -- meaning implement the JNI wrappers yourself -- you actually need to implement 2 sets of wrappers.

JNI will get you to C/C++, which is not allowed to directly access .NET objects. At that point, you can implement another wrapper, a .NET object with only static methods, that your C/C++ wrapper can call. Since unmanaged C/C++ code can't own .NET objects, it can only call static methods of .NET classes.

查看更多
闹够了就滚
4楼-- · 2019-03-22 05:49

Exactly JNI will not give you direct access to .NET unless you work a lot on this. You can build own wrappers and use C/C++ as middle-ware but for small projects it will never pay off. Remember that calling method is one thing but passing arguments in proper types, retrieving results, subscribing events etc..etc.. is a lot more.

Therefore I would also propose to check for third-party tools which are well prepared for these kind of scenarios.

First check at least these two:

Javonet I would recommend for all small and quick projects as this is light solution which provide very high performance due to in process communication and does all background work for you. All you need is call "AddReference(your.dll)" and next invoke any method using reflection-style API. You can invoke any methods, set/get fields and properties, get results, subscribe events or handle exceptions.

Very similar way works JNBridge which has a lit bit more additional staff/extensions for popular enterprise scenarios like cloud integration or websphere but this one I would recommend for bigger projects were you expect to bridge java and .net on separate machines it's more powerful but more complicated and heavy as well.

Both are free to try and Javonet is free for non-commercial and academic usage, so try, test and choose what best suits your requirements.

查看更多
登录 后发表回答