Java use library . DLL

Asked

Viewed 905 times

5

I’m having trouble creating the java interface, because I can’t understand the header of the function in C.

Example Header function C:

CMOEP_API char * CALLCONV CMP_GetLastError( );

Now in java I have like this but I don’t know how to implement the methods:

public class DllFIles {


    public static void main(String[] args) {
     CMOEP lib = new CMOEP();
        System.out.println("ERRO: " +lib.CMP_GetLastError());

    }

}
class CMOEP{
    static {
        System.loadLibrary("CMOEP");
    }
    public native char[] CMP_GetLastError( );
    //    CMOEP_API char * CALLCONV CMP_GetLastError( );

    public CMOEP(){}
}

Any ideas/help?

  • You can learn in this tutorial from Caelum http://blog.caelum.com.br/escrevendo-metodos-nativos-em-java-com-jni-e-jna/

  • What is the name of your . dll file? In your signature of the method in C, it seems to me that you are returning a char, and in the native call it seems that you are declared char[].

  • A. dll belongs to the company and serves to access a database, so the name is not important, I am still not using it completely because I lack the data access to BD, however as I have never used a library . dll wanted to start realizing for when you have to use it to be much easier...

1 answer

3


You can use this Github project to make your life easier: https://github.com/twall/jna

Step by step:

  1. Create a Maven Project
  2. Create the source C
  3. Create the Java interface
  4. Create the Java class

Sources:

dependency on pom.xml

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.1.0</version>
    </dependency>

Code C

#include <stdio.h>

int soma(int num1, int num2){
  int resultado = num1 + num2;
  return resultado;
}

Java interface

package jna;

import com.sun.jna.Library;

public interface SomaJNA extends Library {
  public int soma(int num1, int num2);
}

Java class

package jna;

import com.sun.jna.Native;

public class FazSoma {
    public static void main(String[] args) {
        SomaJNA calculadora = (SomaJNA) Native.loadLibrary("somadorJNA",
                SomaJNA.class);

        int num1 = Integer.parseInt(args[0]);
        int num2 = Integer.parseInt(args[1]);

        int resultado = calculadora.soma(num1, num2);
        System.out.println("A soma é: " + resultado);
    }
}

As in Project Eclipse

inserir a descrição da imagem aqui

That’s All !

  • Thank you very much, for your example seems simple, at the moment I can not test, but when it works I mark as certain ;), only a question I do not know how to use Maven unfortunately but if you download and then the Import of the library " et.java.dev.jna " works as certain?

  • 1

    It is worth learning to use Maven. It is very useful and very productive and is worth a few hours on it. You can always download the JNA but what if you have other dependencies ? You will have to leave by downloading the other Jars. Maybe you’ll find them in this link: https://github.com/twall/jna/tree/master/dist.

  • Thank you @João Paraná, as for Maven I am aware of its importance, I tried to understand how it works but I couldn’t, I have to try again...

  • You can always download the JNA but what if you have other dependencies ? You will have to leave by downloading the other Jars. Maybe you’ll find them in this link: https://github.com/twall/jna/tree/master/dist. Make a first attempt by downloading only the JAR https://github.com/twall/jna/raw/master/dist/jna.jar and see if there are any errors

  • OK thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.