jna中出现错误解决方案。

为什么会出现这些错误之JNA版本的锅

  可能是我们在参考或使用一些老的项目提供的demo时候,用到了jna版本比较低。比如海康的SDK或者一些星浩喇叭的SDK需要和设备对接的项目等…

不要手动导入jar包,使用maven

  好处是不用自己打入包,第二就是老版本中提供的jna没有支持像:linux-aarch64这种版本。

 <!-- 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>

切换高版本会出现的错误

Structure.getFieldOrder() on class 错误

  原因: 高版本需要重写getFieldOrder()方法
  报什么错,你就按照报错的顺序在对应的静态类中加上
这里我遇到一个坑就是加上@Structure.FieldOrder({})后发现解析的数据对不上。如果你也遇到了,请一定安装类中属性顺序去写。特别是星浩的喇叭!

@Structure.FieldOrder({"xxx1", "xxx2" })

  以下是一个示例:

@Structure.FieldOrder({"AlmInMask", "AlmInState", "AlmOutMask", "AlmOutState", "dwTermID", "eTermState"})
    public static class TSdkPostTermState extends Structure {
        public int dwTermID;
        public int eTermState;
        public byte AlmInMask; 
        public byte AlmInState;  
        public byte AlmOutMask;  
        public byte AlmOutState; 
    }

新增第二种解决方案。对于无法使用注解的形式:直接重写其方法

	    public static class TSdkTime extends Structure {
        public short Year;   // 年 : 2018 ~ 2999
        public byte Month;  // 月 : 1 ~ 12
        public byte Day;    // 日 : 1 ~ 31
        public short Hour;   // 时 : 0 ~ 23
        public byte Minute; // 分 : 0 ~ 59
        public byte Second; // 秒 : 0 ~ 59
        public int MillSecond; // 毫秒 : 0 ~ 999
        @Override
        public List getFieldOrder(){
            return Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second", "MillSecond");
        }
    }

java.lang.IllegalArgumentException: Invalid calling convention 63 错误

原因:
  1、接口继承了StdCallLibrary,而不是Library。
  2、接口中用到了回调函数,回调函数继承了StdCallLibrary.StdCallCallback。
  简单来说就是你开发平台是windows,你的线上环境是linux
错误示例:

#错误示例
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;

public interface CtsSdk extends StdCallLibrary {

	public interface OnCtsSdkCallBack extends StdCallCallback {
	 	public int invoke(int eCbType, Pointer pParam, int dwSize, int 		usr_data) throws IOException, ClassNotFoundException;
	}
}

正确示例:

#正确示例
public interface CtsSdk extends Library {
	    
	 public interface OnCtsSdkCallBack extends Callback {
        public int invoke(int eCbType, Pointer pParam, int dwSize, int usr_data) throws IOException, ClassNotFoundException;
    }
}

#参考
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. Community and Support All questions should be posted to the jna-users Google group. Issues can be submitted here on Github. When posting to the mailing list, please include the following: What OS/CPU/architecture you're using (e.g. Windows 7 64-bit) Reference to your native interface definitions (i.e. C headers), if available The JNA mapping you're trying to use VM crash logs, if any Example native usage, and your attempted Java usage It's nearly impossible to indicate proper Java usage when there's no native reference to work from. For commercial support, please contact twalljava [at] java [dot] net. Using the Library 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 Primary Documentation (JavaDoc) The definitive JNA reference is in the JavaDoc. Developers Contributing to JNA Setting up a Windows Development Environment Setting up an Android Development Environment Setting up a RaspberryPi Development Environment Setting up a Mac Development Environment Releasing JNA Publishing to Maven Central Contributing You're encouraged to contribute to JNA. Fork the code from https://github.com/java-native-access/jna and submit pull requests. For more information on setting up a development environment see Contributing to JNA. If you are interested in paid support, feel free to say so on the jna-users mailing list. Most simple questions will be answered on the list, but more complicated work, new features or target platforms can be negotiated with any of the JNA developers (this is how several of JNA's features came into being). You may even encounter other users with the same need and be able to cost share the new development. License This library is licensed under the LGPL, version 2.1 or later, and (from version 4.0 onward) the Apache Software License, version 2.0. Commercial license arrangements are negotiable. NOTE: Oracle is not sponsoring this project, even though the package name (com.sun.jna) might imply otherwise.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值