【问题标题】:JNI error Exception in thread "main" java.lang.UnsatisfiedLinkError: (Ljava/lang/String;)Ljava/lang/String;线程“主”java.lang.UnsatisfiedLinkError 中的 JNI 错误异常:(Ljava/lang/String;)Ljava/lang/String;
【发布时间】:2020-08-05 21:27:25
【问题描述】:

我有一个本地方法可以显示给定路径的文件所有者。

public class ACL{
    static {
      System.loadLibrary("owner"); // hello.dll (Windows) or libhello.so (Unixes)
   }
    // Declare native method
    private native String displayFileOwner(String path);
    
    public static void main(String[] args) {
      String str = "C:/Users/pradeep-pt3689/eclipse-workspace/Permission/jni/owner.c";
      System.out.println(":"+new ACL().displayFileOwner(str));
    }
}

运行时显示此错误的代码

线程“main”中的异常 java.lang.UnsatisfiedLinkError: ACL.displayFileOwner(Ljava/lang/String;)Ljava/lang/String; 在 ACL.displayFileOwner(本机方法) 在 ACL.main(ACL.java:10)

本机代码为:

#include <jni.h>
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include "ACL.h"
#include "accctrl.h"
#include "aclapi.h"

using namespace std;

JNIEXPORT jstring JNICALL Java_ACL_displayFileOwner(JNIEnv *env, jobject obj, jstring path)
{
jstring jstr=NULL;
const char *str=env->GetStringUTFChars(path, NULL);
DWORD dwRtnCode = 0;
PSID pSidOwner = NULL;
BOOL bRtnBool = TRUE;
LPTSTR AcctName = NULL;
LPTSTR DomainName = NULL;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD = NULL;

// Get the handle of the file object.
hFile = CreateFile(
                  str,
                  GENERIC_READ,
                  FILE_SHARE_READ,
                  NULL,
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL,
                  NULL);

// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
          DWORD dwErrorCode = 0;

          dwErrorCode = GetLastError();
          jstr = env->NewStringUTF("CreateFile error.\n");
          return jstr;
}

// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(
                  hFile,
                  SE_FILE_OBJECT,
                  OWNER_SECURITY_INFORMATION,
                  &pSidOwner,
                  NULL,
                  NULL,
                  NULL,
                  &pSD);

// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
          DWORD dwErrorCode = 0;

          dwErrorCode = GetLastError();
          jstr = env->NewStringUTF("GetSecurityInfo error.\n");
          return jstr;
}

// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
                  NULL,           // local computer
                  pSidOwner,
                  AcctName,
                  (LPDWORD)&dwAcctName,
                  DomainName,
                  (LPDWORD)&dwDomainName,
                  &eUse);

// Reallocate memory for the buffers.
AcctName = (LPTSTR)GlobalAlloc(
          GMEM_FIXED,
          dwAcctName);

// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
          DWORD dwErrorCode = 0;

          dwErrorCode = GetLastError();
          jstr = env->NewStringUTF("GlobalAlloc error.\n");
          return jstr;
}

    DomainName = (LPTSTR)GlobalAlloc(
           GMEM_FIXED,
           dwDomainName);

    // Check GetLastError for GlobalAlloc error condition.
    if (DomainName == NULL) {
          DWORD dwErrorCode = 0;

          dwErrorCode = GetLastError();
          jstr = env->NewStringUTF("GlobalAlloc error.\n");
          return jstr;

    }

    // Second call to LookupAccountSid to get the account name.
    bRtnBool = LookupAccountSid(
          NULL,                   // name of local or remote computer
          pSidOwner,              // security identifier
          AcctName,               // account name buffer
          (LPDWORD)&dwAcctName,   // size of account name buffer
          DomainName,             // domain name
          (LPDWORD)&dwDomainName, // size of domain name buffer
          &eUse);                 // SID type

    // Check GetLastError for LookupAccountSid error condition.
    if (bRtnBool == FALSE) {
          DWORD dwErrorCode = 0;

          dwErrorCode = GetLastError();

          if (dwErrorCode == ERROR_NONE_MAPPED)
              jstr = env->NewStringUTF("Account owner not found for specified SID.\n");
          else
                jstr = env->NewStringUTF("Error in LookupAccountSid.\n");
          return jstr;

    } else if (bRtnBool == TRUE)

        {// Print the account name.
        jstr = env->NewStringUTF(AcctName);
        env->ReleaseStringUTFChars(path, str);
    }
    return jstr;
}

我在 web 项目中实际使用的相同 jni 方法来获取文本字段中的文件路径和打印所有者,它工作得非常好。 enter image description here

【问题讨论】:

  • 用dumpbin检查你的dll导出的符号。编辑您的问题,向我们展示所有包含 displayFileProperty 的符号
  • 好的,这听起来很奇怪,但我尝试使用字符串返回类型编译相同类型的方法,它在不同的项目名称下工作得非常好。我什至尝试将仅简单的代码 sn-p 复制到上面的 C 源文件中,它仍然会引发相同的错误。我还是不明白是什么问题。
  • Java 抱怨缺少符号,所以用 dumpbin 检查符号。这会告诉你发生了什么。
  • 你把你的班级放在不同的包里了吗?将相应的 C 代码添加到您的问题和 Botje 所说的 dumpbin/nm 输出中,并在您的 Java 代码中包含包名称。
  • 我尝试在另一台机器上运行相同的代码,它可以工作,但这里显示无法加载 dll 文件。如何检查垃圾箱?我没用过也没听过。 @Botje

标签: java java-native-interface


【解决方案1】:

仍然面临此问题的任何人。请尝试执行一个简单的 JNI HelloWorld 程序,看看它是否有效。在我的情况下,传递字符串和返回名称的程序工作得很好,而传递文件路径和返回所有者的相同方法类型不起作用。

JNIEXPORT jstring JNICALL Java_TestJNIString_sayHello(JNIEnv *env, jobject thisObj, jstring inJNIStr) {
  
   const char *inCStr = (*env)->GetStringUTFChars(env, inJNIStr, NULL);
   return (*env)->NewStringUTF(env, inCStr);
}

如果你有的话,现在尝试添加不同的简单方法,在同一个 JNI 代码中使用上述方法,看看它是否有效。我有一个复杂的方法,它返回一个返回类型为字符串数组的文件的 ACL。我尝试执行一种不同的方法,将星期几作为字符串数组返回,它可以工作。

现在在执行上述 JNI 方法后,更改方法的逻辑,但不更改返回类型和传递的参数。

这很奇怪,但确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2022-01-21
    • 2016-02-07
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多