java - 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 天了,我失去了任何帮助,非常感谢。
解决方案
从你的错误信息来看:
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
推荐阅读
- android - 如何在Android中设计TextView右侧的小点
- java - java中csv中的连字符更改为斜线
- ruby-on-rails - 登录后如何重定向回链接
- laravel - 如何在关系中获取属性或方法(laravel - eloquent)
- ruby-on-rails - Rails CSV 生成器 - 2019 年 6 月 17 日星期一 14:16:10 CEST +02:00:Time in 的未定义方法“收集”
- c# - 使用 linq 在 MVC5 中搜索
- angular - Kendo Angular UI Gauges 更新后自行调整大小
- reactjs - React-toolbox 日期选择器日历对话框正在将 DOM 移到顶部
- java - JiraRestClient 搜索未返回 JQL 查询的结果
- php - 使用 Floyd-Steinberg 2 像 PHP 中的 GIMP 一样抖动将 PNG 保存为 PNG-8