Java 访问 C++ 方法:JavaCPP

摘要:
JavaCPP提供了在Java中高效访问本地C++的方法。采用JNI技术实现,支持所有Java实现包括Android系统,Avian和RoboVM。JavaCPP提供了一系列的Annotation将java代码映射到C++代码,并使用一个可执行的jar包将C++代码转化为可以从JVM内调用的动态链接库文件。

JavaCPP提供了在Java中高效访问本地C++的方法。采用JNI技术实现,支持所有Java实现包括Android系统,Avian 和 RoboVM。

JavaCPP提供了一系列的Annotation将java代码映射到C++代码,并使用一个可执行的jar包将C++代码转化为可以从JVM内调用的动态链接库文件

Maven:

<dependency>

<groupId>org.bytedeco</groupId>

<artifactId>javacpp</artifactId>

<version>0.11</version>

</dependency>

复制代码

使用方法:

C++:

#include <string>

namespace LegacyLibrary {

class LegacyClass {

public:

const std::string& get_property() { return property; }

void set_property(const std::string& property) { this->property = property; }

std::string property;

};

}

复制代码

Java:

import org.bytedeco.javacpp.*;

import org.bytedeco.javacpp.annotation.*;

@Platform(include="LegacyLibrary.h")

@Namespace("LegacyLibrary")

public class LegacyLibrary {

public static class LegacyClass extends Pointer {

static { Loader.load(); }

public LegacyClass() { 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()

LegacyClass l = new LegacyClass();

l.set_property("Hello World!");

System.out.println(l.property());

}

}

复制代码

免责声明:文章转载自《Java 访问 C++ 方法:JavaCPP》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇XSS攻击常识及常见的XSS攻击脚本SQL 入门教程:拼接字段下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java系统属性与Preferences API的简单介绍

系统属性在和Preferences API都是键值对,前者只能当前应用程序中共享数据,而后者可以在用户的各个应用或用户之间共享数据。 系统属性 Java 的系统属性决定了 Java 程序实际运行的环境,默认情况下,JVM 启动时采用系统默认属性值。系统属性顾名思义是指与用户程序相关的操作系统配置信息以及软件信息,位于java.lang包中。 设计到的方法:...

C#生成简单验证码

我们平时无论是网站登录还是注册,都会频繁的遇到各式各样的验证码 ,其实生成验证码对于C#来说非常简单。 下面就是我学习生成验证码的简单实例。 封装的辅助类代码,如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System...

ASP.Net Core 中使用Zookeeper搭建分布式环境中的配置中心系列一:使用Zookeeper.Net组件演示基本的操作

前言:马上要过年了,祝大家新年快乐!在过年回家前分享一篇关于Zookeeper的文章,我们都知道现在微服务盛行,大数据、分布式系统中经常会使用到Zookeeper,它是微服务、分布式系统中必不可少的分布式协调框架。它的作用体现在分布式系统中解决了配置中心的问题,以及解决了在分布式环境中不同进程之间争夺资源的问题,也就是分布式锁的功能以及分布式消息队列功能等...

解决Flink消费Kafka信息,结果存储在Mysql的重复消费问题

背景 最近项目中使用Flink消费kafka消息,并将消费的消息存储到mysql中,看似一个很简单的需求,在网上也有很多flink消费kafka的例子,但看了一圈也没看到能解决重复消费的问题的文章,于是在flink官网中搜索此类场景的处理方式,发现官网也没有实现flink到mysql的Exactly-Once例子,但是官网却有类似的例子来解决端到端的仅一...

洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes

题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent secretary, has been recording the exact wording...

java property 配置文件管理工具框架,避免写入 property 乱序

property property 是 java 实现的 property 框架。 特点 优雅地进行属性文件的读取和更新 写入属性文件后属性不乱序 灵活定义编码信息 使用 OO 的方式操作 property 文件 支持多级对象引用 变更日志 ChangeLog 快速开始 环境依赖 Maven 3.x Jdk 1.7+ Maven 引入依赖 <de...