How to call .NET dll from Java

I have this code to create a simple .NET .dll . It returns only int .

But it does not work inside Java.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ReturnINT { public class ReturnINT { public static int RetornaInteiro () { try { int number = 2; return number; } catch (Exception) { return 1; } } } } 

How can I call a method from Java?

When I use JNI, I have this IN java error:

 Exception in thread "main" java.lang.UnsatisfiedLinkError: Dll.RetornaInteiro()V at Dll.RetornaInteiro(Native Method) at Dll.main(Dll.java:27) 
+4
source share
2 answers

You can call it directly: http://jni4net.sourceforge.net/

Or you can call it executable.

+4
source

Check out http://www.javonet.com . With a single jar file, you can load this DLL and call it like this:

 Javonet.AddReference("your-lib.dll"); int result = Javonet.getType("ReturnINT").Invoke("RetornaInteiro"); 

Javonet will automatically load your library in the .NET process and give you access to any classes and types contained in it. Then you can get your type and call the static method. Method results and arguments are automatically translated between the JAVA and .NET types. You can pass an example string or bool arguments like

 Boolean arg1 = true; String arg2 = "test"; Javonet.getType("ReturnINT").Invoke("MethodWithArguments",arg1,arg2); 

And they will be translated automatically.

In addition, you can also create an instance of your type, subscribe to events, set / receive properties and fields, handle exceptions, or even pass arguments of type value. Check the docs for more details:

http://www.javonet.com/quick-start-guide/

PS: I am a member of the Javonet team. So feel free to ask me any detailed questions regarding native integration and our product.

+3
source

Source: https://habr.com/ru/post/1414232/


All Articles