简体   繁体   English

JNI不满意是什么?

[英]JNI unsatisfiedLinkError?

I am practising JNI and created the shared library file Samplelib.so. 我正在练习JNI并创建了共享库文件Samplelib.so。 I added the directory in which the library file is created, to the java.library.path and when I run the java file, I get java.lang.UnsatisfiedLinkError. 我将创建库文件的目录添加到java.library.path中,当我运行java文件时,我得到了java.lang.UnsatisfiedLinkError。 Here is my Sample.java. 这是我的Sample.java。

import java.util.*;
public class Sample{
public native int intmethod(int n);
public native String stringmethod(String s);
public static void main(String[] args) {
    try{
    //System.setProperty( "java.library.path", "/home/sudhagar/Project" );
    System.load("libSample");
    Sample sample=new Sample();
    int sq=sample.intmethod(2);
    String text=sample.stringmethod("JAVA");
    System.out.println(sq);
    System.out.println(text);
}
catch(UnsatisfiedLinkError e){
    String property = System.getProperty("java.library.path");
    StringTokenizer parser = new StringTokenizer(property, ";");
    while (parser.hasMoreTokens()) {
    System.err.println(parser.nextToken());
    }
}
}
}

My Sample.c file, 我的Sample.c文件,

#include "Sample.h"
#include <string.h>

JNIEXPORT jint  JNICALL Java_Sample_intmethod
(JNIEnv *env, jobject obj, jint n){
    return n*n;
}

JNIEXPORT jstring JNICALL Java_Sample_stringmethod
(JNIEnv *env, jobject obj, jstring n){
    return n;
}   

void main(){}   

I used following command to create the create the shared library, gcc -shared -I/.../include -I/.../include/linux -o libSample.so Sample.c 我用以下命令创建了创建共享库,gcc -shared -I /.../ include -I /.../ include / linux -o libSample.so Sample.c

Use 采用

  System.loadLibrary("Samplelib");

instead of 代替

System.load("Samplelib");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM