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 build and run a basic app that uses the Server Gateway SDK to connect to a channel.

To build and run the app, take the following steps:

  1. Create a Maven project and integrate the SDK.

  2. Go 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 add the following code. Replace Your Token with a valid temporary token and Your App ID with 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(); // Load the JNI library
            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 must match the one you used to generate the temporary token. The code connects to test_channel. If you generated the token for a different channel, replace test_channel with that channel name.

  4. In my-app, create a lib folder. Download the linux-sdk JAR file from Maven Central and extract it 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 number in the URL and the file name is the SDK version. Replace it with the version that you integrate. For the latest version, see the release notes.

  5. In my-app, add the lib/native/linux/x86_64 folder to 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

    When the app connects to the channel, the console prints the following:

    join success