首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

!用eclipse编写JNI程序出现UnsatisfiledLinkError错误

2013-07-01 
求助!用eclipse编写JNI程序出现UnsatisfiledLinkError异常这两天在学习JNI。用家里的电脑在eclipse上编写没

求助!用eclipse编写JNI程序出现UnsatisfiledLinkError异常
这两天在学习JNI。用家里的电脑在eclipse上编写没有出问题。但是换了台电脑就出问题了。。最简单的Jni HelloWorld编译不通过。以下是java部分的代码:


package jni;
 
public class HelloWorld {
    static{
        System.loadLibrary("helloworld");
    }
 
    public native void dispHelloWorld();
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODOAuto-generated method stub
        //System.getProperty("java.library.path");
        new HelloWorld().dispHelloWorld();
    }
 
}


按照网上查到的JNI编写步骤进行,在第一步就出问题了。最开始编译这个java文件就不能通过。正常情况应该是编译通过以后用编译得到的class文件生成.h文件吧。
出现异常:
Thread[main](Suspended(exception UnsatisfiledLinkError))
  ClassLoader.loadLibrary(Class, String, boolean) line: not available
  Runtime.loadLibrary0(Class, String) line: not available
  System.loadLibrary(String) line: not available
  HelloWorld.<clinit>() line: 5
我试了一下用cmd执行javac命令可以编译通过。可是用eclipse就老出问题。求大神解答!!我已经被这个问题折磨很久了。。非常感谢。 Eclipse JNI Java
[解决办法]
这是源代码
// Invoked in the java.lang.Runtime class to implement load and loadLibrary.
    static void loadLibrary(Class fromClass, String name,
    boolean isAbsolute) {
        try {
            if (!DownloadManager.isJREComplete() && 
                    !DownloadManager.isCurrentThreadDownloading()) {
                DownloadManager.downloadFile("bin/" + 
                    System.mapLibraryName(name));
                // it doesn't matter if the downloadFile call returns false --
                // it probably just means that this is a user library, as 
                // opposed to a JRE library
            }
        } catch (IOException e) {


            throw new UnsatisfiedLinkError("Error downloading library " + 
                                                name + ": " + e);
        } catch (NoClassDefFoundError e) {
            // This happens while Java itself is being compiled; DownloadManager
            // isn't accessible when this code is first invoked.  It isn't an
            // issue, as if we can't find DownloadManager, we can safely assume
            // that additional code is not available for download.
        }
        ClassLoader loader =
    (fromClass == null) ? null : fromClass.getClassLoader();
        if (sys_paths == null) {
    usr_paths = initializePath("java.library.path");
    sys_paths = initializePath("sun.boot.library.path");
        }
        if (isAbsolute) {
    if (loadLibrary0(fromClass, new File(name))) {
        return;
    }
    throw new UnsatisfiedLinkError("Can't load library: " + name);
}
if (loader != null) {
    String libfilename = loader.findLibrary(name);
    if (libfilename != null) {
        File libfile = new File(libfilename);
        if (!libfile.isAbsolute()) {
    throw new UnsatisfiedLinkError(
    "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
}
if (loadLibrary0(fromClass, libfile)) {
    return;
}
throw new UnsatisfiedLinkError("Can't load " + libfilename);
    }
}
for (int i = 0 ; i < sys_paths.length ; i++) {
    File libfile = new File(sys_paths[i], System.mapLibraryName(name));
    if (loadLibrary0(fromClass, libfile)) {
        return;
    }
}
if (loader != null) {
    for (int i = 0 ; i < usr_paths.length ; i++) {
        File libfile = new File(usr_paths[i],
System.mapLibraryName(name));
if (loadLibrary0(fromClass, libfile)) {


    return;
}
    }
}
// Oops, it failed
        throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
    }



需要具体的错误信息。
如果错误是throw new UnsatisfiedLinkError("no " + name + " in java.library.path");

1 在java.library.path没有找到,可以将dll或者so设置到该系统变量java.library.path中。
2 或者将dll或者so放到system path中(系统的path环境变量,windows(c:\\windows\\system32), linux(/usr/lib 64位/usr/lib64))
[解决办法]
很明显是没有找到你的dll文件,你新建一个java工程的时候,把dll直接copy到项目下就可以了,然后System.loadLibrary("helloworld");

热点排行