JNA applications

  First, understand the JNA before, we first look at the former JNA JNI (Java Native Interface): through the use of  Java Native Interface written procedures, to ensure that the code can be easily ported on different platforms.  [1]  From Java1.1 start, JNI become part of the standard java platform, which allows Java code and other code written in language interact.

  Implementation process:

  

 

  Note the Writing named jni is required here can refer to: https://baike.baidu.com/item/JNI/9412164?fr=aladdin# writing step

  Two, JNA (Java Native Access): providing a set of Java-based tools for accessing the dynamic local library system (native library: The Window of dll) during operation without writing Native / JNI code. As long as developers in a java interfaces describing function and structure of the target native library, JNA automatically implement Java interfaces to native function mapping.

  Briefly, based jna is encapsulated in a lot jni api, using the above in respect jni is much simplified.

  Implementation process:

  

  Three, JNA examples:

  1) write c source code (sum.cpp)

extern "C" {
    int sum(int x, int y) {
        return x + y;
    }
}

   2) loaded into * .dll / *. So file

  windows

gcc -shared -o <dll_name> <c_name>

  linux

gcc -fpic -shared -o <so_name> <c_name>

  Note: windows environment, the use of windows environment I. c package, can easily take the name, the file format to a dll. c linux environment using a linux package to lib (name) .so are named.

  3) Directory Structure

  

 

   4) maven dependency (the pom.xml)

      <dependency>
            <groupId>com.sun.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.0.9</version>
        </dependency>

  5) implementation process

package com.cetc.util;

import com.sun.jna.Library;
import com.sun.jna.Native;

import java.io.File;

public class SumUtil {

    public interface Sum extends Library {

        int sum(int x, int y);

        Sum INSTANCE = (Sum) Native.loadLibrary("sum", Sum.class);
    }

    static {
        File file = new File("src/main/resources/lib/sum.dll");
        System.load(file.getAbsolutePath());
    }

    public static void main(String[] args) {
        System.out.println("***************************");
        System.out.println(Sum.INSTANCE.sum(1,2));
        System.out.println("***************************");
    }
}

  6) test results

  

 

  IV Summary: JNA largely surface simplifies the connection between different languages, by way of dynamic libraries, to provide to the Java call. General usage scenarios for high performance requirements of the scene, such as opencv like.

    Note: In the actual development projects, not directly to the dll / so file into the jar, because System.load, loaded is the absolute path, can be placed in the Java include or use. Assembly packaging methods.

   V. Source: https://github.com/lilin409546297/JNA

Guess you like

Origin www.cnblogs.com/ll409546297/p/11534886.html