1.引入pom
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.2.0</version>
</dependency>
2.JnaDemo
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform; public class JnaDemo {
public interface CLibrary extends Library {
//调用动态链接库c://windows/system32/msvcrt.dll中的printf函数输出hello world
CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),CLibrary.class);
void printf(String format, Object... args);
} public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");//调用printf函数 }
}
3.输出结果
Hello, World Process finished with exit code