我该如何解决“不满意的链接错误”? JNI [英] How Can I Resolve "Unsatisfied Linkerror" Jni

查看:66
本文介绍了我该如何解决“不满意的链接错误”? JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到一个异常不满意的错误即



线程main中的异常java.lang.UnsatisfiedLinkError: test.HelloWorld.print()V

at test.HelloWorld.print(Native Method)

at test.HelloWorld.main(HelloWorld.java:24)



我使用Netbeans7.1和Eclipse链接Java和C ++



请参阅下面的代码..



Java部分

*************

  package  test; 
public class HelloWorld
{
static
{
// System.load(C :\\\\\\\\\ HelloHorld \\Debug \\libHelloWorld.dll);
System.loadLibrary( libHelloWorld);
}
私有 native void print();

/ * *
* @param args命令行参数
* /

public static void main( String [] args)
{
// 这里的TODO代码应用程序逻辑

new HelloWorld()。print( );

}
}





C ++ - ECLIPSE

** *********



hello_jni.h

************ ****

  / *  请勿编辑此内容文件 - 由机器生成* /  
#include < jni.h >
< span class =code-comment> / * 类helloworld_HelloWorld的标题* /

#ifndef _Included_test_HelloWorld
#define _Included_test_HelloWorld
#ifdef __cplusplus
extern C {
#endif
/ *
*类:helloworld_HelloWorld
*方法:打印
*签名:()V
* /

JNIEXPORT void JNICALL Java_test_HelloWorld_print(JNIEnv *,jobject);

#ifdef __cplusplus
}
#endif
#endif





hello_jni.cpp

******************

 include   hello_jni.h 
使用 命名空间标准;
/ *
*类:hello_jni.cpp
*方法:print
*签名:()V
* /


JNIEXPORT void JNICALL Java_test_HelloWorld_print(JNIEnv * env,jobject jobj)
{
std :: cout<< Iside C ++ native method HELLOOOOOOOOOOOOOOOOOO< /跨度>;
return ;
}





请帮助

提前致谢。

解决方案

我有一段时间没有触及JNI,但我相信你的C ++库是静态链接到jni.dll的。程序运行时,它无法加载您的DLL,因为系统无法在默认搜索路径中找到jni.dll,因此无法加载您的DLL。所以检查一下。


我怀疑你没有 java.library.path 的默认路径,所以java找不到DLL。我刚刚构建了你的项目,将DLL复制到与类文件相同的目录中......

 C:\ Users \ Richard \Documents \\目录\\ Java \ jni2 \ test 

27/02/2015 14:45< dir> 。
27/02/2015 14:45< dir> ..
27/02/2015 14:28 463 HelloWorld.class
27/02/2015 14:27 481 HelloWorld.java
27/02/2015 14:44 37,376 libHelloWorld.dll
3档案38,320字节
2 Dir(s)123,847,589,888字节免费
< / dir>< / dir>



...并键入命令...

 java -Djava.library.path = C:\ Users \ Richard \Documents \ Java \ jni2\test test / HelloWorld 



收到结果

 Iside C ++原生方法HELLOOOOOOOOOOOOOOOOOOOO 


Hi
Iam getting an Exception "UnsatisfiedLinakge error" ie,

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

Iam using Netbeans7.1 and Eclipse to link Java and C++

Please see the below code..

Java Part
*************

package test;
public class HelloWorld
{    
    static
    {
       // System.load("C:\\eclipse\\ws\\HelloWorld\\Debug\\libHelloWorld.dll");
        System.loadLibrary("libHelloWorld");
    }
    private native void print();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
          
       new HelloWorld().print();
                       
    }
}



C++ - ECLIPSE
***********

"hello_jni.h"
****************

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

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

#ifdef __cplusplus
}
#endif
#endif



"hello_jni.cpp"
******************

include "hello_jni.h"
using namespace std;
/*
 * Class:     hello_jni.cpp
 * Method:    print
 * Signature: ()V
 */

JNIEXPORT void JNICALL Java_test_HelloWorld_print(JNIEnv * env, jobject jobj)
{
	std::cout<<"Iside C++ native method HELLOOOOOOOOOOOOOOOOOOO";
	return;
}



Please Help
Thanks in advance.

解决方案

I haven't touched JNI for a while, but I believe your C++ library is statically linked to jni.dll. When your program is running it cannot load your DLL because the system cannot find jni.dll in the default search path and as a result your DLL cannot be loaded. So check that.


I suspect that you do not have a default path for java.library.path so java cannot locate the DLL. I just built your project, copied the DLL to the same directory as the class file ...

 Directory of C:\Users\Richard\Documents\Java\jni2\test

27/02/2015  14:45    <dir>          .
27/02/2015  14:45    <dir>          ..
27/02/2015  14:28               463 HelloWorld.class
27/02/2015  14:27               481 HelloWorld.java
27/02/2015  14:44            37,376 libHelloWorld.dll
               3 File(s)         38,320 bytes
               2 Dir(s)  123,847,589,888 bytes free
</dir></dir>


... and typed the command ...

java -Djava.library.path=C:\Users\Richard\Documents\Java\jni2\test test/HelloWorld


receiving the result

Iside C++ native method HELLOOOOOOOOOOOOOOOOOOO


这篇关于我该如何解决“不满意的链接错误”? JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆