DLL’s communication with Java

Asked

Viewed 470 times

10

I am layman on the subject of how to communicate with DLL’s using the JAVA platform. Would you like to know where to start studying, if there is any framework? I’ve heard of JNI but I don’t know if it’s right to use.

Any documentation, link, framework, api and etc are welcome.

  • The way is the JNI yes, but unfortunately I know little of it. There is also the JNA. Keep my +1 because I also have an interest in this.

  • Here’s an example using JNI and JNA. http://blog.caelum.com.br/scrindo-metodos-nativos-em-java-com-jni-e-jna/

1 answer

5


As commented by Victor Stafusa, two common alternatives for consuming native code are the JNI (standard Java engine) and JNA (a popular library to consume C libraries).

In a rough simplification we can say that the main difference between the two is that with JNI most of the "mapping" is done on the C/C++ application side. With JNA mapping is done on the Java side.

JNA is an abstraction about the convenient JNI to work with C; mapping C++ code ends up giving a lot of work and is not always possible.

JNI

When using JNI you have a development process that usually consists of:

  1. Create a class with native marked methods (native) in Java.
  2. Compile the class (e. g., javac)
  3. Create headers and stubs C/C++ for the class in question (using javah)
  4. Write the implementation of "bridge" in C/C++. for example, delegating calls to your dll original
  5. Create a shared library with the code you wrote (e. g., libPonte.dll)
  6. Load the native library (e.g., using System.loadLibrary)

Here are some links about JNI:

  1. IBM developerWorks - Java Programming with JNI
  2. Oracle Blogs | Moonocean - A simple example of JNI
  3. Oracle - Java Native Interface Specification

JNA

Already the integration using JNA usually starts from the headers library documents, following the following process:

  1. Identify native functions you want to call.

  2. Include the archive jna.jar in the classpath of the application

  3. Declare a mapping interface on the Java side (inheriting from Library) or a class with methods native and direct mapping. The methods of this interface / class must be a counterpart of the C functions you want to call.

  4. Map structures, function pointers and other particularities. Structs in C are mapped to Java classes that inherit from Structure. C function pointers are mapped to Java classes that inherit from Callback.

    • Optionally steps 3 and 4 can be automated. Tools such as Jnaerator are capable of generating the mapping of the dll and of headers library.
  5. Load native library with methods loadLibrary or register

Here are some links about JNA:

  1. Calling C code from Java using JNA
  2. Getting Started with JNA
  3. JNA API Documentation

It is necessary to mention that there are other libraries with the same purpose of JNA; the choice of library usually involves a trade-off between simplicity and control. Honorable mention for the SWIG capable of generating interfaces for multiple languages (including Java / through JNI) and Bridj which sells as an alternative to the JNA with "real" support to the C++.

Diving a little deeper you find the legacy world of distributed objects, including libraries like JACOB (for WITH) and Jacorb (for CORBA) but this is a subject for another question (specific about CPI).

Browser other questions tagged

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