5
回答
JNI提示找不到依赖的库文件,java.lang.UnsatisfiedLinkError。
注册华为云得mate10,2.9折抢先购!>>>   
E:\MySource\app_test\NativeTest>java HelloNativeTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: E:\MySource\app_test\NativeTest\HelloNative.dll: Can't find d
ependent libraries
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at HelloNativeTest.<clinit>(HelloNativeTest.java:4)
Could not find the main class: HelloNativeTest.  Program will exit.
运行JNI测试程序,提示如上。 下面是程序,按照Java核心技术第八版上的实例程序来的:
class HelloNative
{
	public static native void greeting();
}
javac HelloNative.java
javah HelloNative
生成如下C的头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloNative */

#ifndef _Included_HelloNative
#define _Included_HelloNative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloNative
 * Method:    greeting
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloNative_greeting
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

编写C文件:
#include "HelloNative.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloNative_greeting(JNIEnv* env, jclass cl)
{
	printf("Hello Native World\n");
	}

在Windows7 64位平台上使用MinGW编译该程序生成dll文件,命令如下:
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at  -I%JAVA_HOME%/include -I%JAVA_HOME%/include/win32
  -shared HelloNative.c -o HelloNative.dll

在当前目录下生成了HelloNative.dll文件,编写测试main方法:
class HelloNativeTest{
	
	static{
			System.loadLibrary("HelloNative");
		}
	
		public static void main(String args[]){
				HelloNative.greeting();
			}
	}

运行之后就出现了上面的异常,我试过将dll文件路径放入PATH中,试过将dll文件放入System32中,均出现此异常。 

请熟悉该问题的朋友,帮忙看下哪里有问题,3Q。

举报
BenjaminMa
发帖于6年前 5回/17K+阅
顶部