JAN使用技法

2022-03-14 11:29
310
0

 

项目地址:http://jna.java.net/   
API: http://jna.java.net/javadoc/overview-summary.html

JNA全称Java Native Access,是一个建立在经典的JNI技术之上的Java开源框架(https://github.com/twall/jna)。JNA提供一组Java工具类用于在运行期动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。

 

参考

https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md

 

    

package com.sun.jna.examples;
    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]);
            }
        }
    }
    

参考网址:

https://blog.csdn.net/gwd1154978352/article/details/55097376

全部评论