博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JNI_OnLoad调用时机
阅读量:5930 次
发布时间:2019-06-19

本文共 2955 字,大约阅读时间需要 9 分钟。

 终于建了一个自己个人小站:,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~

       JNI_OnLoad是在加载so的时候调用的,也就是System.loadLibrary("test")时候调用的。具体调用步骤如下:

System.loadLibrary(libName)->Runtime.loadLibrary(libName,classLoader)->Runtime.doLoad(libName,loader)->Runtime.nativeLoad(name,loader,ldLibraryPath)->JavaVMExt.loadNativieLibrary:(JavaVMExt* vm=Runtime::Current()->GetJavaVM(); vm->LoadNativieLibrary(filename,classloader,detail))->jni_internal.cc::LoadNativieLibrary->dlopen,dlsym->JNI_OnLoad。

以上给出了调用顺序,具体代码可以根据类名或方法名看出。有兴趣的可以自己查看下源码。

在Runtime.loadLibrary中,会在多个多个路径下寻找对应的so,只要找到了就会去加载,加载成功就会返回。对应代码如下:

Runtime.java

/*     * Searches for and loads the given shared library using the given ClassLoader.     */    void loadLibrary(String libraryName, ClassLoader loader) {        if (loader != null) {            String filename = loader.findLibrary(libraryName);            if (filename == null) {                // It's not necessarily true that the ClassLoader used                // System.mapLibraryName, but the default setup does, and it's                // misleading to say we didn't find "libMyLibrary.so" when we                // actually searched for "liblibMyLibrary.so.so".                throw new UnsatisfiedLinkError(loader + " couldn't find \"" +                                               System.mapLibraryName(libraryName) + "\"");            }            String error = doLoad(filename, loader);            if (error != null) {                throw new UnsatisfiedLinkError(error);            }            return;        }        String filename = System.mapLibraryName(libraryName);        List
candidates = new ArrayList
(); String lastError = null; for (String directory : mLibPaths) { String candidate = directory + filename; candidates.add(candidate); if (IoUtils.canOpenReadOnly(candidate)) { String error = doLoad(candidate, loader); if (error == null) { return; // We successfully loaded the library. Job done. } lastError = error; } } if (lastError != null) { throw new UnsatisfiedLinkError(lastError); } throw new UnsatisfiedLinkError("Library " + libraryName + " not found; tried " + candidates); }
mLibPaths初始化代码如下:

/**     * Holds the library paths, used for native library lookup.     */    private final String[] mLibPaths = initLibPaths();    private static String[] initLibPaths() {        String javaLibraryPath = System.getProperty("java.library.path");        if (javaLibraryPath == null) {            return EmptyArray.STRING;        }        String[] paths = javaLibraryPath.split(":");        // Add a '/' to the end of each directory so we don't have to do it every time.        for (int i = 0; i < paths.length; ++i) {            if (!paths[i].endsWith("/")) {                paths[i] += "/";            }        }        return paths;    }

转载地址:http://ceutx.baihongyu.com/

你可能感兴趣的文章
deis安装
查看>>
黑帽大会:有150种方法可绕过Web应用防火墙!
查看>>
Yii CDbCriteria 常用方法
查看>>
python中获取当前运行函数名称的方法
查看>>
linux自学心得之--安装内核头文件
查看>>
linux ssh 报错 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
查看>>
keepalived+lvs实现mysql集群读的操作(写类似)
查看>>
Elasticsearch from+size 超过10000结果解决方案(V 2.x)
查看>>
php学习笔记--序
查看>>
再次学习的回忆
查看>>
我的友情链接
查看>>
LINUX下网站维护命令
查看>>
F5+IIS7.5 SNAT日志记录真实源IP
查看>>
【 Visual C++】游戏开发笔记之二——最简单的DirectX,vc窗口的编写
查看>>
我的友情链接
查看>>
LDAP架构部署认证
查看>>
Linux网络抓包分析工具Tcpdump基础篇[参数说明]
查看>>
00_02启动tomcat时 一闪而过解决方法
查看>>
WSUS 客户端无法提示更新!
查看>>
在处理文件服务器上的文件时文件服务器性能下降并出现延迟
查看>>