java通过jna调用so
c++:
FirstEliteValidate.h
#pragma once
void __attribute__((constructor)) startup();
void __attribute__((destructor)) shutdown();
#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 -Djava.library.path=lib com.bg.security.Program4Linux
桂棹兮兰桨,击空明兮溯流光。