Search code examples
agora.io

java.lang.UnsatisfiedLinkError when using agora.io Server Gateway Java SDK


I am building a simple demo with agora.io Server Gateway Java SDK. I am using maven that downloads the lib linux-sdk-4.0.1.jar. https://mvnrepository.com/artifact/io.agora.rtc/linux-sdk/4.0.1 Inside the jar I see the linux lib so.

    0  02-06-2023 11:59   native/
        0  02-06-2023 11:59   native/linux/
        0  02-06-2023 11:59   native/linux/x86_64/
   739728  02-06-2023 11:59   native/linux/x86_64/libagora-fdkaac.so
 17865952  02-06-2023 11:59   native/linux/x86_64/libagora_rtc_sdk.so
  3472144  02-06-2023 11:59   native/linux/x86_64/libagora-ffmpeg.so
  1556224  02-06-2023 11:59   native/linux/x86_64/libbinding.so

The source code

package com.example;

import io.agora.rtc.AgoraService;
import io.agora.rtc.AgoraServiceConfig;
import io.agora.rtc.AgoraRtcConn;
import io.agora.rtc.RtcConnConfig;
import io.agora.rtc.VideoDimensions;
import io.agora.rtc.AgoraLocalVideoTrack;
import io.agora.rtc.AgoraVideoFrameSender;
import io.agora.rtc.VideoEncoderConfig;
import io.agora.rtc.Constants;

public class Main {
    // static {
    //     System.load("/app/ext_lib/libagora_rtc_sdk.so"); // Ensure the native library is loaded
    // }

    private static final String APP_ID = "YOUR_APP_ID";
    private static final String APP_CERTIFICATE = "YOUR_APP_CERTIFICATE";
    private static final String CHANNEL_NAME = "YOUR_CHANNEL_NAME";
    private static final String UID = "12345"; // Set your UID or use 0 for automatic assignment

    public static void main(String[] args) {
        System.out.println("App started");
        try {
            // Initialize AgoraService
            AgoraServiceConfig serviceConfig = new AgoraServiceConfig();
            serviceConfig.setAppId(APP_ID);
            serviceConfig.setAreaCode(0);
            AgoraService agoraService = new AgoraService();
            agoraService.initialize(serviceConfig);

            // Create RTC connection
            RtcConnConfig rtcConnConfig = new RtcConnConfig();
            AgoraRtcConn rtcConn = agoraService.agoraRtcConnCreate(rtcConnConfig);

            // Generate token if necessary
            String token = generateToken(APP_ID, APP_CERTIFICATE, CHANNEL_NAME, UID);

            // Join the channel
            rtcConn.connect(token, CHANNEL_NAME, UID);
            long cptr = 0;
            // Create and configure local video track
            AgoraVideoFrameSender videoFrameSender = new AgoraVideoFrameSender(cptr);
            AgoraLocalVideoTrack localVideoTrack = agoraService.createCustomVideoTrackFrame(videoFrameSender);

            // Set video encoder configuration
            VideoDimensions dimensions = new VideoDimensions(640, 360);
            int codecType = Constants.VIDEO_CODEC_H264;
            int frameRate = 24;
            int bitrate = 0;
            int minBitrate = 0;
            int orientationMode = Constants.VIDEO_ORIENTATION_0;
            int degradationPreference = 0;
            int mirrorMode = Constants.VIDEO_MIRROR_MODE_AUTO;
            VideoEncoderConfig config = new VideoEncoderConfig(
                codecType,
                dimensions,
                frameRate,
                bitrate,
                minBitrate,
                orientationMode,
                degradationPreference,
                mirrorMode
            );
            localVideoTrack.setVideoEncoderConfig(config);
            // Publish the local video track
            rtcConn.getLocalUser().publishVideo(localVideoTrack);

            // Leave the channel after use
            rtcConn.disconnect();
            agoraService.destroy();
            // agoraService.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String generateToken(String appId, String appCertificate, String channelName, String uid) {
        // Token generation logic here, similar to the previous example
        // This method should return a valid token
        return "YOUR_TOKEN";
    }
}

but when executing, the error happens.

java -cp target/agora-example-1.0-SNAPSHOT.jar:target/lib/linux-sdk-4.0.1.jar com.example.Main
App started
Exception in thread "main" java.lang.UnsatisfiedLinkError: 'long io.agora.rtc.AgoraService.init()'
        at io.agora.rtc.AgoraService.init(Native Method)
        at io.agora.rtc.AgoraService.<init>(AgoraService.java:16)
        at com.example.Main.main(Main.java:31)

Where did I make the mistake?

I also checked the so file. It is likely not for Java JNI

1:16:35 ext_lib $ objdump -T libagora_rtc_sdk.so | grep Java_

 1:16:44 ext_lib $ //nothing related to Java was output

I also tried the version 3.7.200.21 but it is the same.


Solution

  • The java SDK uses JNI so I suspect you need to run:

    export LD_LIBRARY_PATH=lib/native/linux/x86_64/:$LD_LIBRARY_PATH

    for the JNI bindings to work correctly since it is looking for those files and expects the path to be in there.