java - 如何使用JNA在java代码中使用dll文件?

标签 java c eclipse exception dll

我必须在我的java代码中使用一个dll文件(c代码),我搜索了allot并发现JNA是最合适的方式。因此,我尝试使用 jna-4.1.0.jar 编写一个 HelloWorld 程序。下面是c代码:

//C hello world example
#include <stdio.h>

void __declspec(dllexport) _stdcall helloFromC()
{
  printf("Hello world from the c code! \n");
}

我通过在cmd上使用以下命令为这个c代码制作dll:

gcc -c ctest.c
gcc -shared -0 test.dll ctest.o

运行这些命令会为我提供一个 C 代码的 dll,我将其放置在简单的 Java 项目的根目录中。

现在这是我的 java 类:

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

public class HelloWorld {

public interface CTest extends Library {
    void helloFromC();
}

 public static void main(String[] args) {
        CTest INSTANCE = (CTest) Native.loadLibrary("ctest", CTest.class);

        INSTANCE.helloFromC();
    }
}

现在,当我在 eclipse 上运行这个 java 程序时,我收到此错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.

请帮我解决这个问题,因为它已经花了很多时间!

最佳答案

gcc -c ctest.c
gcc -shared -o test.dll ctest.o
try     'o'  ^  not '0'

它正在寻找您的 dll,但它不喜欢它。

关于java - 如何使用JNA在java代码中使用dll文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862722/

相关文章:

java - 在 Eclipse 上运行基本字数统计程序时出错

Java/ Kotlin : Finding the intersection of multiple HashSets by class ID

java - 从文件中打印出正确的数据

java - 如何在 GWT Java 中获取当前工作目录

c++ - 如何在 OpenCV 中使用 cv::createButton 原型(prototype)

c++ - makefile 混淆中的链接器库路径

c - 如何根据 C 编程语言标准初始化结构体

java - JSF f :view beforePhase firing multiple times as I LEAVE the page

android - mvn 安卓 :deploy -> Unfortunately App has stopped

android - 无法阻止 Eclipse 提示我已经修复的错误