[英]Using DLL in java through JNA
我正在使用JNA在Java中使用dll,但出现以下错误
线程“主”中的异常java.lang.UnsatisfiedLinkError:查找函数'GetStatus'时出错:找不到指定的过程。
没有得到如何解决这个问题?
请帮忙。
这是java代码
import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native library declaration and usage. */
public class First {
public interface TEST extends Library {
public String GetStatus();
}
public static void main(String[] args) {
TEST obj = (TEST ) Native.loadLibrary("TEST ", TEST .class);
System.out.println( obj.GetStatus());
}
}
这款Nugget超级易用,并且效果完美。 https://www.nuget.org/packages/UnmanagedExports
您需要Visual Studio 2012(快速)。 安装后,只需在要导出的任何静态函数之前添加[RGiesecke.DllExport.DllExport]
。 而已!
例:
C#
[RGiesecke.DllExport.DllExport]
public static int YourFunction(string data)
{
/*Your code here*/
return 1;
}
爪哇
在顶部添加导入:
import com.sun.jna.Native;
在您的班级中添加接口。 它是您的C#函数名称,后跟字母“ I”:
public interface IYourFunction extends com.sun.jna.Library
{
public int YourFunction(String tStr);
};
在类中需要的地方调用您的DLL:
IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));
编辑:如果DLL是32位的,则JDK / JRE也必须是32位的。 将以下检查添加到您的代码中:
if(!System.getProperty("os.arch").equals("x86")) {
throw new Exception(".NET DLL " + "32bits JRE/JDK is required. Currently using " + System.getProperty("os.arch") + ".\r\nTry changing your PATH environement variable.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.