JNIアプリケーションの例をたくさん見てきました。自分で試して例外が発生しました
dileepvikram@dileepvikram-System-Product-Name:~/include$ java -Djava.Library.path=. Test
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Test in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Test.<clinit>(Test.java:9)
Could not find the main class: Test. Program will exit.
Test.java
public class Test {
public native void sayHello(int length) ;
public static void main (String args[]) {
String str = "I am a good boy" ;
Test h = new Test () ;
h.sayHello (str.length() ) ;
}
static {
System.loadLibrary ( "Test" ) ;
}
}
私は彼のコードをコンパイルし、そのコードでTest.hを作成しました
javah -jni Test
Test.c
#include "Hello.h"
#include<stdio.h>
#include "jni.h"
JNIEXPORT void JNICALL Java_hello_sayHello
(JNIEnv *env, jobject object, jint len) {
printf ( "\nLength is %d", len ); }
void main()
{
printf("\nHello World\n");
}
次に、コマンドを使用してcコードをコンパイルしました
gcc Test.c -o libTest.so
次に、コマンドを使用してJavaクラスを実行してみました
java -Djava.library.path=. Test
そして、私は例外を取得しています
dileepvikram@dileepvikram-System-Product-Name:~/include$ java -Djava.Library.path=. Test
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Test in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Test.<clinit>(Test.java:9)
Could not find the main class: Test. Program will exit.
私は多くのことを試みました、問題を見つけるために、どんな助けもありがたいです。