java通过jna调用so
Posted 空明流光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java通过jna调用so相关的知识,希望对你有一定的参考价值。
c++:
FirstEliteValidate.h
#pragma once #ifdef __cplusplus extern "C" { #endif int FirstEliteValidateTest(); #ifdef __cplusplus } #endif
FirstEliteValidate.cpp
#include "FirstEliteValidate.h" /* To test the library, include "FirstEliteValidate.h" from an application project and call FirstEliteValidateTest(). Do not forget to add the library to Project Dependencies in Visual Studio. */ static int s_Test = 0; int FirstEliteValidateTest() { return ++s_Test; }
编译出so文件命名为 libFirstEliteValidate.so,复制到 /usr/lib下
java:
package com.bg.security; import java.util.Properties; import com.sun.jna.Library; import com.sun.jna.Native; public class Program4Linux { public interface CLibrary extends Library { int FirstEliteValidateTest(); } public static void main(String[] args) { CLibrary lib = (CLibrary) Native.load("FirstEliteValidate", CLibrary.class); int a = lib.FirstEliteValidateTest(); int b = lib.FirstEliteValidateTest(); int c = lib.FirstEliteValidateTest(); System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); Properties props = System.getProperties(); System.out.println("Operation System:" + props.getProperty("os.name")); System.out.println("System Version:" + props.getProperty("os.version")); System.out.println("end"); } }
运行:
java -Djava.ext.dirs=/home/knife/DemoCode/jar com.bg.security.Program4Linux
以上是关于java通过jna调用so的主要内容,如果未能解决你的问题,请参考以下文章
JNA 调用c++函数的出错,java能加载库,但是找不到c++写的函数