【问题标题】:JNI : Get java.lang.UnsatisfiedLinkErrorJNI:获取 java.lang.UnsatisfiedLinkError
【发布时间】:2014-10-26 10:03:34
【问题描述】:

这个问题已经被问了好几次,并且有不同的接受答案。但它们都不适合我。
我已经从我的项目中制作了一个 jni dll,以便在 64x windows 7 中使用 eclipse 和 jdk1.7.0_10(64 位)。但在加载我的 DLL 后,我得到 java.lang.UnsatisfiedLinkError.
我开始基于这个guide 创建一个helloworld 项目。我做它所说的一切。但我已经收到此错误:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: test.HelloWorld.print()V
    at test.HelloWorld.print(Native Method)
    at test.HelloWorld.main(HelloWorld.java:24)

是的,我已经包含了库路径是的,我已经为 x64 构建了 C 项目是的,我正在使用 64 位 jvm。

代码:

package test;

public class HelloWorld {
    public native void print();  //native method
    static   //static initializer code
    {
        try{
            System.loadLibrary("CLibHelloWorld");

               }
               catch (UnsatisfiedLinkError e) {
                  System.err.println("Native code library failed to load.\n" + e);
                  System.exit(1);
                }

    } 

    public static void main(String[] args)
    {
        HelloWorld hw = new HelloWorld();
        hw.print();             //   ==>  i get error on this line
    }
}

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

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

#include "HelloWorld.h"
#include "jni.h"
#include  "stdio.h"

JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
  printf("Hello world\n");
  return;
}

【问题讨论】:

  • 出于好奇,因为它可能会帮助您调试问题,如果您尝试使用JavaCPP,它是否有效?
  • @SamuelAudet 我会试试的

标签: java c++ c eclipse java-native-interface


【解决方案1】:

test.HelloWorld.print 的 C 名称是

Java_test_HelloWorld_print

您缺少_test - 您是否使用默认包中的类运行 javah?

【讨论】:

  • 在我的项目中,我有如下功能: Java_Secure_Key_connect 所以在 java 中我创建了名为 test 的项目,然后是一个名为 Secure 的包b> 带有一个名为 key 的类,并在该类中调用本机 connect 函数。这应该工作还是我犯了错误?
  • 我认为名字是区分大小写的,所以类需要是Key 否则我认为没关系。
【解决方案2】:

你错过了包名test 你应该在 c 中包含你的包名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多