用Java加载静态编译的库 [英] Load statically compiled library in Java

查看:141
本文介绍了用Java加载静态编译的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用System.loadLibrary(")从Java加载静态编译的库,但是可以将其作为动态编译的库(当我以这种方式构建时)进行加载.我使用的是JDK 8,如果您在* .cpp和* .h文件中提供了JNI_OnLoad_L,则我的理解是它可以通过System.loadLibrary(")加载静态库.

I'm having trouble loading a statically compiled library from Java using System.loadLibrary("") but I can load it as a dynamically compiled library (when I build it that way) just fine. I'm using JDK 8 and my understanding is it can load static libraries via System.loadLibrary("") if you provide a JNI_OnLoad_L in the *.cpp and *.h files.

我的kdu_jni.h有:

My kdu_jni.h has:

extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad_kdu_1jni(JavaVM *, void *);

我的kdu_jni.cpp具有:

My kdu_jni.cpp has:

JNIEXPORT jint JNICALL JNI_OnLoad_kdu_1jni(JavaVM *vm, void *reserved)
{
  return JNI_VERSION_1_8;
}

当我尝试以编译版本运行时,我的java.library.path目录中有libkdu_jni.a文件.当我尝试动态加载它时,使用同一目录中的libkdu_jni.so文件可以正常工作.尝试使用静态文件(libkdu_jni.a)时,我得到:

I have the libkdu_jni.a file in my java.library.path directory when I try to run with the compiled version. It's working fine with a libkdu_jni.so file in that same directory when I try to load it dynamically. When trying with the static file (libkdu_jni.a), I get:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no kdu_jni in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)

在尝试加载.a文件之前,我已取出.so文件.

I've taken out the .so file before trying to load the .a file.

我不确定自己在做什么错.我认为它甚至没有看到libkdu_jni.a文件的JNI_OnLoad_kdu_1jni(),因为我在其中放置了一个异常,并且看不到该异常.我已经尝试过对该名称进行多次迭代:JNI_OnLoad_kdu_jni(),JNI_OnLoad_kdu_1jni(),JNI_OnLoad()等.

I'm not sure what I'm doing wrong. I don't think it's even seeing the libkdu_jni.a file's JNI_OnLoad_kdu_1jni() because I put an exception in there and I don't see that getting thrown. I've tried several iterations on that name: JNI_OnLoad_kdu_jni(), JNI_OnLoad_kdu_1jni(), JNI_OnLoad(), etc.

有什么想法吗?

推荐答案

我的理解是,如果您在* .cpp和* .h文件中提供JNI_OnLoad_L,它可以通过System.loadLibrary(")加载静态库.

my understanding is it can load static libraries via System.loadLibrary("") if you provide a JNI_OnLoad_L in the *.cpp and *.h files.

您的理解不正确.您不能动态加载.a文件.它不能以任何形式执行:

Your understanding is incorrect. You can't load a .a file dynamically. It isn't executable in any way shape or form:

  • 馆内参考文献未解决
  • 库外部的引用也无法解析:例如,对C库的引用.

链接步骤是必不可少的,JVM不会帮您完成.您已阅读的内容适用于静态链接到JVM的库.

The link step is essential, and the JVM doesn't do it for you. What you have read applies to libraries statically linked into the JVM.

这篇关于用Java加载静态编译的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆