gpt4 book ai didi

java - 如何访问静态方法?

转载 作者:行者123 更新时间:2023-11-30 11:24:17 25 4
gpt4 key购买 nike

我想使用 Java 和 JNA 库访问商业 DLL 文件 (C#) 中的静态方法。

不幸的是,Java 7 不允许在接口(interface)中使用静态方法,尽管 Java 8 会对此进行修改,但最新的测试版似乎对此无动于衷。

欢迎提出建议/更正(我是 JNA 的新手,正在避免使用 JNI),并且我已经使用 Javonet 来确认方法签名。

import com.sun.jna.Library;
import com.sun.jna.Native;

public class INeedHelp {
public interface MyInterface extends Library {
public static boolean isDisconnected(); //Mirror of C# method signature, but wont work
public boolean isDisconnected(); //best fit, but throws exception "Error looking up function 'isDisconnected': The specified procedure could not be found."
}

public static void main(String[] args) {
MyInterface anInstance = (MyInterface) Native.loadLibrary("theDLL", MyInterface.class);
anInstance.isDisconnected();
}
}

最佳答案

您应该能够使用 Direct Mapping , 类似;

import com.sun.jna.*;

public class MyClass {

public static native boolean isDisconnected();

static {
Native.register("theDLL");
}

public static void main(String[] args) {
MyClass.isDisconnected();
}
}

关于java - 如何访问静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20709050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com