linux下java调用.so动态库方法2: JNA
摘自:http://blog.csdn.net/todorovchen/article/details/21319033
另请参见: http://blog.sina.com.cn/s/blog_8cfbb9920100zy7g.html
LINUX 下 JNA 调用 so--正确版
项目中需要用到Java调用c++,了解过JNI,但比较复杂,后来看到JNA(JNI的加强版)。
网上看了很多例子,但是始终出错,主要错误原因是undefined symbol,找不到c++ 方法。
教程的有些细节没说(- -||),好吧,我把成功的例子贴一下吧。
1.编写C++ so库
c++代码:注意加上extern “C”,否则无法找到c++方法。
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- extern "C"
- {
- void test() {
- cout << "TEST" << endl;
- }
- int addTest(int a,int b)
- {
- int c = a + b ;
- return c ;
- }
- }

我把so文件放到了 /lib 下。
2.JAVA代码
import com.sun.jna.Library;
import com.sun.jna.Native; public class jnatest1 { // 继承Library,用于加载库文件
public interface Clibrary extends Library {
// 加载libhello.so链接库
Clibrary INSTANTCE = (Clibrary) Native.loadLibrary("hello",
Clibrary.class); // 此方法为链接库中的方法
void test();
int addTest(int a,int b);
} public static void main(String[] args) {
// 调用
Clibrary.INSTANTCE.test();
int c = Clibrary.INSTANTCE.addTest(10,20);
System.out.println(c);
}
}
linux下java调用.so动态库方法2: JNA的更多相关文章
- linux下java调用.so文件的方法1: JNI
摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...
- linux下java调用C
linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...
- Java调用dll动态库
最近项目里使用java调用dll动态库,因此研究了一下这方面的东西. 使用的工具包如下 <dependency> <groupId>net.java.dev.jna</g ...
- linux下,一些关于动态库的问题:
程序运行是加载动态库的几种方法: 第一种,通过ldconfig命令 ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令它,ldconfig命令通 ...
- C#调用C++动态库方法及动态库封装总结
我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...
- java调用C++ DLL库方法
最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...
- Linux下编译tinyxml生成动态库
首先去到sourceforge下载tinyxml的源码,https://sourceforge.net/projects/tinyxml/?source=dlp,最新版本是2.6.2. 将下载成功的t ...
- Linux下gcc编译控制动态库导出函数小结
根据说明文档“How To Write Shared Libraries"介绍, 有四种方法: 1. 在方法声明定义时,加修饰:__attribute__((visibility(" ...
- java使用JNA框架调用dll动态库
这两天了解了一下java调用dll动态库的方法,总的有三种:JNI.JNA.JNative.其中JNA调用DLL是最方便的. ·JNI ·JNA ·JNative java使用 JNI来调用dll动态 ...
随机推荐
- 异步套接字基础:select函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
参考:[原创]技术系列之 网络模型(三)多路复用模型 select函数 select函数: 系统提供select函数来实现多路复用输入/输出模型.原型: #include <sys/time.h ...
- Bin & Jing in wonderland(概率,组合数学)
Problem 2103 Bin & Jing in wonderland Accept: 201 Submit: 1048 Time Limit: 1000 mSec Memor ...
- DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法
在学习牛腩老师的JS视频中,视频中的例子要求实现一个是23个3相乘的结果,在用Dreamweaver制作时,, <script language="javascript" t ...
- [ReactJS] DOM Event Listeners in a React Component
React doesn't provide the listener to listen the DOM event. But we can do it in React life cycle: So ...
- SQL记录-字符串的截取与拼接
SELECT concat( substr(t1.CODE, , ), '****', substr(t1.CODE, ) ), t1.CODE, t2.TITLE FROM table1 t1 LE ...
- [Python学习笔记][第六章Python面向对象程序设计]
1月29日学习内容 Python面向对象程序设计 类的定义与使用 类定义语法 使用class关键词 class Car: def infor(self): print("This is ca ...
- 模板页 相对路径 JS 加载问题
问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...
- No redirect found in host configuration file (C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet.config).
Configuration Error Description: An error occurred during the processing of a configuration file req ...
- UILabel自适应高度,自动换行
CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...
- hdu1166 经典线段入门
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...