首页 > java使用jna调用

java使用jna调用

现在有一个获取机器码的c++开发好的dll,java程序需要调用这个dll获取到机器码;
1)、dll里面有个int getMacCode(char *p)这个函数;
2)、让p这个指针指向的地址空间保存了机器码;
3)、用java如何实现这个调用过程?
ps:目前用jna来调用,但是一直报找不到函数的错误,“java.lang.UnsatisfiedLinkError: Error looking up function 'getMacCode':xxxx”;估计认为是参数传递上错误了,请大神赐教


再看了下问题,貌似不是这个原因。可能需要你帖点代码出来才晓得了
下面的东西先留着,等找到真正的原因了再来改吧


看样子是 DLL 没加载到,JNA 的文档里有设置路径的办法

Make your target library available to your Java program. There are several ways to do this:

  • The preferred method is to set the jna.library.path system property to the path to your target library. This property is similar to java.library.path, but only applies to libraries loaded by JNA.
  • Change the appropriate library access environment variable before launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux, and DYLD_LIBRARY_PATH on OSX.
  • Make your native library available on your classpath, under the path {OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for native libraries (e.g. win32-x86, linux-amd64, or darwin). If the resource is within a jar file it will be automatically extracted when loaded.

大概就是需要你把 DLL 所在目录设置在系统的 PATH 路径里,或者通过 java 的参数 jna.library.path 指定目录,

java -Djna.library.path=PATH_TO_YOUR_DLL ClassName

参数应该是byte[]类型的

【热门文章】
【热门文章】