I have this code to create a simple .NET .dll
. It only returns an int
.
But, it is not working 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 the method from within Java?
When I Use JNI i have this error IN java:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Dll.RetornaInteiro()V
at Dll.RetornaInteiro(Native Method)
at Dll.main(Dll.java:27)
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.
SilverStream 3.0 provides a mechanism for calling DLLs directly from Java without writing any native code. The DllLib class in the com.sssw.rt.com package has several static methods that let you load a DLL into memory, get a function pointer, and make a call to the function.
You will need to use the Java Native Interface (JNI), which is a set of C/C++ functions that allow native code to interface with java code (i.e. receiving parameters from java function calls, returning results, etc). Write a wrapper C library that receive JNI calls and then call your external library.
Jni4net is a mechanism that allows a Java program to call a function in a C# program and a C# program to call a method in a Java program. The jni4net framework includes both . NET FCL and JDK core classes to possibly use a Reflection technology implementation across the boundary.
Check the http://www.javonet.com as well. With one-jar file you can load this dll and call as follows:
Javonet.AddReference("your-lib.dll");
int result = Javonet.getType("ReturnINT").Invoke("RetornaInteiro");
Javonet will automatically load your library in .NET process and give you access to any classes and types contain within it. Next you can get your type and invoke static method. Method results and arguments are automatically translated between JAVA and .NET types. You can pass for example string or bool arguments like that
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 instance of your type, subscribe events, set/get properties and fields, handle exceptions or even pass value-type arguments. Check the docs for more details:
http://www.javonet.com/quick-start-guide/
PS: I am member of Javonet team. Therefore feel free to ask me any detailed questions regarding native integrations and our product.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With