Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call into .NET dll from Java

Tags:

java

c#

dll

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)
like image 350
soamazing Avatar asked May 24 '12 19:05

soamazing


People also ask

Can I use .NET DLL in Java?

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.

Can we call DLL from Java?

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.

How can we call external DLL method from Java program?

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.

Can we use Java code in C#?

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.


1 Answers

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.

like image 52
Przemysław Ładyński Avatar answered Sep 19 '22 06:09

Przemysław Ładyński