简体   繁体   English

在Java中使用JNI加载Teamspeak动态链接库

[英]Load Teamspeak dynamic linked library with JNI in Java

Is it possible to load the Teamspeak.so library by System.loadLibrary in java and access the methods declared in the .h files? 是否可以通过Java中的System.loadLibrary加载Teamspeak.so库并访问.h文件中声明的方法? (sources are the files from the ts3_sdk_3.0.3.2 - .h files from /include/teamspeak/serverlib.h; .so files from /bin/libts3server_linux_amd64.so) (来源是ts3_sdk_3.0.3.2中的文件-/include/teamspeak/serverlib.h中的.h文件; /bin/libts3server_linux_amd64.so中的.so文件)

Im able to load the library: 我无法加载库:

System.loadLibrary("ts3server_linux_amd64"); - works without error. -正常运行。

When i try to use a method i get a 当我尝试使用一种方法时,我得到了

java.lang.UnsatisfiedLinkError java.lang.UnsatisfiedLinkError

Testcode: 测试代码:

public class main {

    static {
        System.loadLibrary("ts3server_linux_amd64");

    }

    public static void main(String[] args) {

        new main().onClientStartTalkingEvent();

    }

    private native void onClientStartTalkingEvent();
}

(.so file is stored in a lib folder and added to the classpath. OS is ubuntu). (.so文件存储在lib文件夹中,并添加到类路径中。操作系统为ubuntu)。

Thanks and best regards 谢谢和最好的问候

Instead of loading the teamspeak .so you need to load the .so of your JNI code (which links against the teamspeak .so). 而不是加载teampeak .so,您需要加载JNI代码的.so(链接到teampeak .so的链接)。

Edit 编辑

To call a native library from Java you need to write a JNI wrapper. 要从Java调用本机库,您需要编写一个JNI包装器。 This is a native library itself which you have to load from Java and can be called through native functions. 这是一个本机库本身,您必须从Java加载它,并且可以通过native函数调用。 If the native library you want to call is C (and not C++) you can have a look at projects like jnr-ffi or jna . 如果你要拨打的本地库是C(而不是C ++),你可以看看像项目JNR-FFIJNA These allow you to call C libraries without having to write a JNI wrapper. 这些使您无需编写JNI包装器即可调用C库。

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

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