Java Native Access简介, 解决java.lang.NoClassDefFoundError和cannot find symbol com.sun.jna.Library问题

目录

Introduction to JNA

Who use JNA?

Features

Why JNA

下载jar

Hello World示例

 总结:对比JNI和JNA方法

JNI的方式

JNA的方式


Introduction to JNA

Java Native Access (JNA) is a community-developed library providing Java programs an easy access to native shared libraries (.dll files on windows, .so files on Unix ...)

JNA is a mature library with dozens of contributors and hundreds of commercial and non-commercial projects that use it.

Who use JNA?

Features

  • Automatic mapping from Java to native functions, with simple mappings for all primitive data types
  • Runs on most platforms which support Java
  • Automatic conversion between C and Java strings, with customizable encoding/decoding
  • Structure and Union arguments/return values, by reference and by value
  • Function Pointers, (callbacks from native code to Java) as arguments and/or members of a struct
  • Auto-generated Java proxies for native function pointers
  • By-reference (pointer-to-type) arguments
  • Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer
  • Nested structures and arrays
  • Wide (wchar_t-based) strings
  • Native long support (32- or 64-bit as appropriate)
  • Demo applications/examples
  • Supported on 1.4 or later JVMs, including JavaME (earlier VMs may work with stubbed NIO support)
  • Customizable marshalling/unmarshalling (argument and return value conversions)
  • Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros
  • Support for automatic Windows ASCII/UNICODE function mappings
  • Varargs support
  • Type-safety for native pointers
  • VM crash protection (optional)
  • Optimized direct mapping for high-performance applications.
  • COM support for early and late binding.
  • COM/Typelib java code generator.

Why JNA

JNA(Java Native Access)是一个开源的Java框架,是Sun公司推出的一种调用本地方法的技术,是建立在经典的JNI基础之上的一个框架。之所以说它是JNI的替 代者,是因为JNA大大简化了调用本地方法的过程,使用很方便,基本上不需要脱离Java环境就可以完成。

不需要手写访问java变量和函数的方法 

下载jar

GitHub - java-native-access/jna at 5.10.0Java Native Access. Contribute to java-native-access/jna development by creating an account on GitHub.https://github.com/java-native-access/jna/tree/5.10.0

Hello World示例

示例代码:
 jna/GettingStarted.md at master · java-native-access/jna · GitHubJava Native Access. Contribute to java-native-access/jna development by creating an account on GitHub.https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md

其中代码有些不友好,环境介绍只有一句:

Begin by downloading the latest release of JNA and referencing jna.jar in your project's CLASSPATH

如果不熟悉会出现如下问题:

1. 编译时出现cannot find symbol com.sun.jna.Library问题

需要在javac和java 中指定-classpath

javac -classpath .:jna.jar -g com/sun/jna/examples/HelloWorld.java

java -classpath .:jna.jar com.sun.jna.examples.HelloWorld

2. 运行时出现java.lang.NoClassDefFound问题

问题出在package com.sun.jna.examples

请放文件在如下目录:

/examples

        jna.jar        

         /com    

                /sun      

                        /jna          

                                /examples          

                                         HelloWorld.java

参考java - JNA 示例程序 java.lang.NoClassDefFoundError - IT工具网https://www.coder.work/article/1410839

其他的代码解释:

 参考JNI的替代者—使用JNA访问Java外部功能接口 - Alexia(minmin) - 博客园1. JNA简单介绍先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言(尤其C/C++)写的代码进行交互,只要遵守调用约定即可https://www.cnblogs.com/lanxuezaipiao/p/3635556.html

 总结:对比JNI和JNA方法

JNI的方式

JNA的方式

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值