首页   快速返回

java调用c方法JNA实例     所属分类 java
JNA(Java Native Access)
linux so文件  shared object


jnatest.c  jnatest.o  libjnatest.so

#include
int add(int a, int b);
int add(int a, int b)
{
    int c = a + b;
    // printf("Hello, this method is used to add two Integer.");
    return c;
}

gcc -fpic -c jnatest.c
gcc -shared -o libjnatest.so jnatest.o

JnaTest.java


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

public interface JnaTest extends Library{	
	public static JnaTest INSTANCE = (JnaTest) Native.loadLibrary("jnatest", JnaTest.class, NativeLibrary.getInstance("jnatest").getOptions()); 
    int add(int a, int b);
}

把动态库放到 classpath 目录下 譬如 web应用的  WEB-INF/classes  

MAC    libjnatest.dylib
LINUX  libjnatest.so 


JnaTestMain.java

import java.util.Date;
import dyyx.jna.JnaTest;

public class JnaTestMain {
	
	public static void main(final String[] args) {
		System.out.println("JnaTestMain,"+new Date());		
		System.out.println(JnaTest.INSTANCE.add(2, 3));
	}
}

上一篇     下一篇
ZGC简单介绍

docker常用命令

docker Busybox 实战

java堆外内存回收机制

ConcurrentHashMap在jdk7和8中的区别

优秀投资者的十大特征