java调用本地方法--javacpp简介

本篇结构:

  • 简介
  • 必备软件
  • 使用示例

    一、简介

javacpp 是一种替换 JNI、JNA 的开源技术。它提供了在 Java 中高效访问本地 C++ 的方法。JavaCPP 提供了一系列的 Annotation 将 Java 代码映射到C++代码,并使用一个可执行的 jar 包将 C++ 代码转化为可以从JVM内调用的动态链接库文件。

github地址是:https://github.com/bytedeco/javacpp

二、必备软件

为了正常使用javacpp,需要安装下列软件:

三、使用示例

为了调用本地方法,JavaCPP 生成了对应的 JNI 代码,并且把这些代码输入到 C++编译器,用来构建本地库。使用了 Annotations 特性的 Java 代码在运行时会自动调用 Loader.load() 方法从 Java 资源里载入本地库,这里指的资源是工程构建过程中配置好的。

在一个目录下面,新建一个 NativeLibrary.h 文件,定义了一个 c++ 类:

#include <string>

namespace NativeLibrary {
    class NativeClass {
        public:
            const std::string& get_property() { return property; }
            void set_property(const std::string& property) { this->property = property; }
            std::string property;
    };
}

为了完成javaCPP工作,在同目录下定义一个java类如下:

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

@Platform(include="NativeLibrary.h")
@Namespace("NativeLibrary")
public class NativeLibrary {
    public static class NativeClass extends Pointer {
        static { Loader.load(); }
        public NativeClass() { allocate(); }
        private native void allocate();

        // to call the getter and setter functions 
        public native @StdString String get_property(); public native void set_property(String property);

        // to access the member variable directly
        public native @StdString String property();     public native void property(String property);
    }

    public static void main(String[] args) {
        // Pointer objects allocated in Java get deallocated once they become unreachable,
        // but C++ destructors can still be called in a timely fashion with Pointer.deallocate()
        NativeClass l = new NativeClass();
        l.set_property("Hello World!");
        System.out.println(l.property());
    }
}

当然需要将相应的jar包放到目录中。

然后执行:

javac -cp javacpp.jar NativeLibrary.java // 编译

java -jar javacpp.jar NativeLibrary // 使用一个可执行的 jar 包将 C++ 代码转化为可以从JVM内调用的动态链接库文件,因为我是在linux下执行,所以生成一个linux-x86_64目录,目录下一个libjniNativeLibrary.so文件

java  -cp javacpp.jar NativeLibrary // 运行打印Hello World!

猜你喜欢

转载自blog.csdn.net/w1992wishes/article/details/80283481