JNA即Java Native Access。
官方文档
- Getting Started
- Functional Description.
- Mapping between Java and Native
- Using Pointers and Arrays
- Using Structures and Unions
- Using By-Reference Arguments
- Customization of Type Mapping
- Callbacks/Function Pointers/Closures
- Dynamically Typed Languages (JRuby/Jython)
- Platform Library
- Direct Method Mapping (Optimization)
- Frequently Asked Questions (FAQ)
- Avoiding Crashes
使用样例
准备Java代码文件,内容来源于Getting Started,如下:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.load((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
修改项目的pom.xml
,增加如下依赖:
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.14.0</version>
</dependency>
等待依赖下载完毕,构建完毕,运行前述样例代码。
从源码构建JNA
- Setting up a Windows Development Environment
在Windows环境执行构建,执行如下命令:
在Linux环境执行构建,执行如下命令:cd C:\Code\jna set JAVA_HOME=C:\Tools\jdk set ANT_HOME=C:\Tools\ant set CYGWIN_HOME=C:\Tools\cygwin64 set PATH=%JAVA_HOME%\bin;%ANT_HOME%\bin;%CYGWIN_HOME%\bin;%PATH% "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 ant
cd /path/to/jna_code export JAVA_HOME=/path/to/jdk export ANT_HOME=/path/to/ant export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH ant
- Android Development Environment
在Linux环境执行构建,执行如下命令:cd /path/to/jna_code export JAVA_HOME=/path/to/jdk export ANT_HOME=/path/to/ant export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH export NDK_PLATFORM=/path/to/android-ndk-r12b/platforms/android-21 export PATH=$NDK_PLATFORM/../../toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/:$PATH ant -Dos.prefix=android-aarch64
参考资料
- JNA Examples
可以覆盖日常开发过程中大部分的应用场景。 - JNA实现JAVA调用C/C++动态库
- JNA介绍
- SpringBoot集成JNA调用动态库
- Java 之 JNA(调用第三方库)
- JNA学习(一)C++ 类库(Dll)创建及调用
- java高级用法之:调用本地方法的利器JNA
- JNA (Java 本地访问)理论概述与入门
- JNA介绍以及简单使用
- java高级用法之:在JNA中使用类型映射
- Java调用第三方库JNA(C/C++)
- JNI便捷开发框架JNA框架之入门(一)
- JNI,JNA使用总结
- 记一次JNA踩坑历程 -- JNA调用DLL
- 【Hotspot】 JNI/JNA调用、Native调用
- JNA——模拟c++中类及成员
- JNI,JNA,jawin,jacob和Jcom之间的区别是什么,它们的调用效率怎么排名?
- Java 外部函数接口:JNI, JNA, JNR
- JNA简介及使用
- Java——本地方法(JNA)详解
- Java 之 JNA(调用第三方库)
- Java使用jna调用c++动态库(Linux)