首页 > 解决方案 > Windows JNI 在 dll 错误 java.lang.UnsatisfiedLinkError 中加载和执行 c++ 代码:

问题描述

答案/解决方案:要明确 Jorn Vernee 的答案是正确的,因为当我最初创建我的 .h 文件时,我在 .java 类中没有包信息,但是当我的代码被执行时,它确实有包信息。.h 文件中的方法名称最终会在名称中包含包信息。

我有一些用于截屏的 C++ 代码。我一直在尝试使用 JNA 和 JNI 从 Java 调用它,但都没有运气。我的最终目标是让 captureScreen 方法工作。我创建了一个 getNumber 方法作为一个简单的测试,没有传入任何参数,但即使它也不起作用。

更新我的应用程序作为 64 位应用程序运行,而我的 dll 是 64 位的。不确定这是否会影响传入或传出的参数。

我目前的实现

爪哇

public class JNIScreenShotTest { 
   static {
  String oldLibraryPath = System.getProperty("java.library.path");
  try{
  //This doesnt work for me it fails on the loadLibrary call and I get an UnsatisfiedLinkError
  //System.setProperty("java.library.path", "C:\\workspace\\com.tdkc.udop\dlls");
  //System.loadLibrary("JNIScreenShot"); 
                               
  //This Manages to load the library without error                         
  System.load("C:\\workspace\\com.tdkc.udop\\dlls\\JNIScreenShot.dll")
  }finally}
      System.setProperty("java.library.path", oldLibraryPath );
  }
 }

  private native void captureScreen(String filePath, int x, int y, int width, int height);

  private native int getNumber();

}

我使用 javac -h 从上面的类创建我的头文件

.h 文件

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class JNIScreenShot */

    #include "DXGICapture.h"

    #include <stdio.h>
    #include <tchar.h>
    #include <shlobj.h>
    #include <atlconv.h>
    #include <string>

    using namespace std;

    #ifndef _Included_JNIScreenShot
    #define _Included_JNIScreenShot
    #ifdef __cplusplus
    extern "C" {
    #endif
        /*
         * Class:     JNIScreenShot
         * Method:    captureScreen
         * Signature: (Ljava/lang/String;IIII)V
         */
        JNIEXPORT void JNICALL Java_JNIScreenShot_captureScreen
        (JNIEnv*, jobject, jstring, jint, jint, jint, jint);

        /*
     * Class:     JNIScreenShotTest
     * Method:    getNumber
     * Signature: ()I
     */
        JNIEXPORT jint JNICALL Java_JNIScreenShotTest_getNumber
        (JNIEnv*, jobject);

    #ifdef __cplusplus
    }
    #endif
    #endif

C++ 文件

    // ScreenCapture.cpp : Defines the exported functions for the DLL.
//


#include "JNIScreenShot.h"
#include <iostream>


JNIEXPORT void JNICALL Java_JNIScreenShot_captureScreen
(JNIEnv *env, jobject clz, jstring filePath, jint x, jint y, jint width, jint height) {
    HRESULT hr = S_OK;
    CDXGICapture dxgiCapture;
    jboolean isCopy;
    const char* pszOutputFileName = (env)->GetStringUTFChars(filePath, &isCopy);

    hr = dxgiCapture.Initialize();
    tagScreenCaptureFilterConfig config;

    // set default config
    RtlZeroMemory(&config, sizeof(config));
    config.ShowCursor = 1;
    config.SizeMode = tagFrameSizeMode_AutoSize;

    hr = dxgiCapture.SetConfig(config);
    hr = dxgiCapture.CaptureToFile((LPCTSTR)CA2WEX<>(pszOutputFileName), x, y, width, height);
    if (FAILED(hr))
    {
        printf("Error[0x%08X]: CDXGICapture::CaptureToFile failed.\n", hr);
    }
}

/*
 * Class:     JNIScreenShotTest
 * Method:    getNumber
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_JNIScreenShotTest_getNumber
(JNIEnv *env, jobject clz) {
    return 2;
}

我的java调用如下

File f = new File("Some file");
int x = some value;
int y = some value;
int width = some value;
int height = some value;

JNIScreenShot jniScreenShot = new JNIScreenShot();
jniScreenShot.(f.getAbsolutePath(), x,y,width,height)

这会生成错误 java.lang.UnsatisfiedLinkError: com.tdkc.udop.screencapture.jniScreenShot.captureScreen(Ljava/lang/string;IIII)V

起初我认为我的 captureScreen 中的某些东西失败了,或者可能是我传入的数据类型不同。所以我创建了 getNumber 方法作为测试

调用 getNumber() java.lang.UnsatisfiedLinkError: com.tdkc.udop.screencapture.jniScreenShot.getNumber()I 时出现类似错误

两条错误消息都引用了 .h 文件中的签名,因此我知道它找到了 dll 并尝试调用正确的方法。

花了一些时间查看 UnsatisfiedLinkError 但大多数谷歌搜索结果表明,当库正在加载时,我已经看到很多包括尝试使用 System.loadLibrary() 而不是 System.load()

它似乎可以找到方法,因为错误包含在我的 .h 文件中为每种方法定义的签名。

在这一点上,我已经尝试遵循关于 JNI 的一百万个指南和教程,并且已经 2 天了,我失去了任何帮助,非常感谢。

标签: javac++java-native-interface

解决方案


从你的错误信息来看:

java.lang.UnsatisfiedLinkError: com.tdkc.udop.screencapture.jniScreenShot.getNumber()I

您的班级似乎在一个包中。但是,您的本机函数的名称与此不匹配:

Java_JNIScreenShotTest_getNumber

包名称也应该以本机函数的名称进行编码。从该错误消息中,您的本机函数的名称看起来应该是:

Java_com_tdkc_udop_screencapture_jniScreenShot_getNumber

使用-Xlog:library*=trace自 JDK 15 起)您可以看到 VM 用于查找的确切名称。例如:

[0.211s][info][library] Loaded library C:\Program Files\Java\jdk-15\bin\nio.dll, handle 0x00007ffacf0f0000
...
[0.212s][info][library] Found Java_sun_nio_fs_WindowsNativeDispatcher_initIDs in library with handle 0x00007ffacf0f0000

推荐阅读