简体   繁体   English

JNI:UnsatisfiedLinkError

[英]JNI: UnsatisfiedLinkError

I'm trying to test out some JNI code integrating a Java class with some ROS functionality and I'm struggling to get the Java methods linked up correctly. 我正在尝试测试一些将Java类与某些ROS功能集成在一起的JNI代码,并且正在努力使Java方法正确链接。 I've got the native code compiled against the JNI interface correctly (or so I think) but at runtime I get an UnsatisifiedLinkError on the first native method I have defined. 我已经针对JNI接口正确编译了本机代码(或者,我认为),但是在运行时,我定义的第一个本机方法遇到了UnsatisifiedLinkError At this point I'm not sure if the root cause is that the JVM isn't properly loading the .so file (in the same directory, and I've tried -Djava.library.path=. ) or if it's successfully loading it and it's not finding the method correctly. 此时,我不确定根本原因是JVM是否未正确加载.so文件(在同一目录中,并且我已经尝试过-Djava.library.path=. ),或者是否已成功加载它,并且找不到正确的方法。

This error message gives so little to go on, is there a way to get more info about what exactly is causing it? 此错误消息给您带来的影响不大,是否有办法获取有关确切原因的更多信息?

I'm not opposed to posting the source code if it would help, though I'd have to do some editing before I can upload it so I'll wait to see if you guys think it might be helpful. 我不反对发布源代码是否有帮助,尽管在上传之前我必须做一些编辑,所以我会等着看你们是否认为这可能有所帮助。

Talker.java: Talker.java:

public class Talker {
    /**
     * ROS Native methods
     *
     * Simple passthrough to the C++ native methods in the ROS layer
     */
    private static native void rosAdvertise();
    private static native void rosPublish();
    private static native void rosSpinOnce();

    {
        System.loadLibrary("ros-test-native-talker");
    }

    public static void main(String[] args) {
        rosAdvertise();

        while (true) {
            rosPublish();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            rosSpinOnce();
        }
    }
}

ros-test-native-talker.cpp: ros-test-native-talker.cpp:

#include "test_rostest_Talker.h"
#include "ros/ros.h"
#include "std_msgs/Time.h"

ros::Publisher outbound;


JNIEXPORT void JNICALL Java_test_rostest_Talker_rosAdvertise
    (JNIEnv *, jclass) {
    int argc = 0;
    char **argv;
    ros::init(argc, argv, "ros-native-timing-tester");

    ros::NodeHandle n;
    outbound = n.advertise<std_msgs::Time>("chatter", 1000);
}


JNIEXPORT void JNICALL Java_test_rostest_Talker_rosPublish
    (JNIEnv *, jclass) {
    ros::Time tx_timestamp = ros::Time::now();

    ROS_INFO("Sending message at %d.%d", tx_timestamp.sec, tx_timestamp.nsec);

    std_msgs::Time msg;
    msg.data = tx_timestamp;

    outbound.publish(msg);
}


JNIEXPORT void JNICALL Java_test_rostest_Talker_rosSpinOnce
    (JNIEnv *, jclass) {
    ros::spinOnce();
}

and the output: 和输出:

rush@lubuntu64vm:~/javarostest$ java -Djava.library.path=. -cp ros-test-native-1.0-SNAPSHOT.jar test.rostest.Talker                                                                                            
Exception in thread "main" java.lang.UnsatisfiedLinkError: test.rostest.Talker.rosAdvertise()V
        at test.rostest.Talker.rosAdvertise(Native Method)
        at test.rostest.Talker.main(Talker.java:21)

I have no clue why but refactoring the above code slightly caused it to work. 我不知道为什么,但是重构上面的代码会使它正常工作。 If I take the native methods out of the main classand put them in a separate class (thus removing the static modifier on the native methods) which is invoked by the main class, it all links and works fine. 如果我将本机方法从主类中取出,并将它们放在一个单独的类中(从而删除了本机方法上的static修饰符),该类由主类调用,则所有这些方法都可以正常工作。 I'm not certain nor do I even have a clue why, but I think the static modifier on those methods was causing some issues. 我不确定也不知道为什么,但是我认为这些方法上的static修饰符引起了一些问题。

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

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