【发布时间】:2021-08-28 06:09:45
【问题描述】:
我想调用这个最小的虚拟 C 程序(名为“TEST.c”):
extern "C"
void Java_TEST_run() {}
来自此 Java 代码(名为“Example.java”):
public class Example
{
public static void main(String args[])
{
System.out.println("START");
TEST test = new TEST();
test.dll_call();
System.out.println("ALL DONE!");
}
}
class TEST
{
public void dll_call()
{
run();
}
static {
try {
System.out.println("Load DLL = start ");
System.load("/home/user/Desktop/TEST.dll");
System.out.println("Load DLL = finish ");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n");
System.exit(1);
}
}
public native void run();
}
我通过以下命令创建 C dll 文件:
g++ -c TEST.c
g++ -shared -o TEST.dll TEST.o
这在控制台环境中运行良好,尤其是。我得到了成功的Java程序执行输出:
START
Load DLL = start
Load DLL = finish
ALL DONE!
现在,如果我尝试从 Eclipse IDE 运行 Java 程序,则会收到以下错误:
START
Load DLL = start
Load DLL = finish
Exception in thread "main" java.lang.UnsatisfiedLinkError: 'void Example.TEST.run()'
at test1/Example.TEST.run(Native Method)
at test1/Example.TEST.dll_call(Example.java:21)
at test1/Example.Example.main(Example.java:11)
据我了解,这意味着从 Eclipse 运行的 Java 程序确实成功地找到了 c dll 文件,但是在尝试输入 dll 文件时,由于找到了虚拟函数 Java_TEST_run() 而失败。
由于 c 代码“TEST.c”已经尽可能少,并且从控制台执行正常,我不明白为什么它在 Eclipse 中失败。
谁能给我建议,如何在 Eclipse 中让这个最小的 c 代码在 Java 中工作?
操作系统是 Ubuntu 18.04,openjdk 11.0.6 和 Eclipse 4.14.0。
这是来自question 的后续问题。
非常感谢您的帮助,谢谢!
【问题讨论】:
-
感谢您的评论,格雷格!我最初尝试使用 JNI 语句(包括 C 代码和编译命令中的标头),它在控制台中运行良好,但是当我尝试从 Eclipse 运行 Java 代码时,我遇到了同样的 UnsatisfiedLink 错误。因此,我试图获得一个在 Eclipse 中工作的最小示例。但到目前为止,我未能从 Eclipse 获得任何 DLL 调用。我还尝试了 Eclipse 网络上的各种“hello world”dll 示例,但总是以 unsatisfiedLink 错误告终。