Compile and run the sample project

Updated

Set up, compile, and run the sample Server Gateway project provided by Agora.

This page shows you how to implement a basic Hello World program after integrating Server Gateway SDK.

  1. Create a new maven project and integrate the SDK.

  2. Navigate to my-app/src and remove the test folder.

    rm -r test
  3. Open my-app/src/main/java/com/mycompany/app/App.java and enter the following code. Replace Your Token and Your App ID with a valid Video SDK temporary token and your App ID.

    import io.agora.rtc.SDK;
    import io.agora.rtc.AgoraRtcConn;
    import io.agora.rtc.AgoraService;
    import io.agora.rtc.AgoraServiceConfig;
    import io.agora.rtc.DefaultRtcConnObserver;
    import io.agora.rtc.RtcConnInfo;
    
    public class App {
        public static class ConnObserver extends DefaultRtcConnObserver {
            @Override
            public void onConnected(AgoraRtcConn conn, RtcConnInfo rtcConnInfo, int reason) {
            System.out.println("join success");
            }
        }
    
        public static void main(String[] args) throws Exception {
            String token = "Your Token";
            SDK.load(); // ensure JNI library load
            AgoraService service = new AgoraService();
            AgoraServiceConfig config = new AgoraServiceConfig();
            config.setEnableAudioProcessor(0);
            config.setEnableAudioDevice(0);
            config.setEnableVideo(0);
            config.setContext(0);
            config.setAppId("Your App ID");
            service.initialize(config);
    
            AgoraRtcConn conn = service.agoraRtcConnCreate(null);
    
            conn.registerObserver(new ConnObserver());
    
            conn.connect(token, "test_channel", "1");
    
            Thread.sleep(2000);
            conn.disconnect();
            conn.destroy();
            service.destroy();
        }
    }

    The channel name you use to connect to the Video SDK channel must be the same as the one you used to generate your Video SDK temporary token. In the following code, the channel name is set to "test_channel". If you use a different channel name to generate your Video SDK temporary token, replace "test_channel" with your own channel name.

  4. Navigate to my-app and create a lib folder. Get the linux-sdk jar file from maven, and extract the files to the lib folder.

    mkdir lib
    cd lib
    wget https://repo1.maven.org/maven2/io/agora/rtc/linux-sdk/4.0.1/linux-sdk-4.0.1.jar
    jar xvf linux-sdk-4.0.1.jar

    The version field in dependencies is the SDK version number, which needs to be updated according to the SDK version you integrate. For the latest SDK version number, refer to the release notes.

  5. Navigate to my-app and add the lib/native/linux/x86_64 folder to the LD_LIBRARY_PATH.

    export LD_LIBRARY_PATH=lib/native/linux/x86_64/:$LD_LIBRARY_PATH
  6. Build the project.

    mvn package
  7. Run the app.

    java -cp target/agora-rtc-linux-java-1.0-SNAPSHOT.jar App

    The console prints the following information.

    $ java -cp target/agora-rtc-linux-java-1.0-SNAPSHOT.jar App
    join success