Last Updated: 8/19/2022, 10:39:25 PM

# Java SDK Getting Started Guide

# Overview

You can use AccelByte Cloud’s Java SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. This guide will show you how to create a project which uses Java Server SDK from scratch.

# Prerequisites

  • AccelByte Cloud (demo environment):
    • Use https://demo.accelbyte.io (opens new window) for AB_BASE_URL environment variable.
    • Create an Oauth Client with client type Confidential.
    • Use Client ID value for AB_CLIENT_ID environment variable.
    • Use Client Secret value for AB_CLIENT_SECRET environment variable.
  • Java Development Kit (JDK) 8
  • Gradle 7
  • Java IDE, e.g., Visual Studio Code

# Tutorial

# Create a Java Project

  1. Create a folder and use gradle init to create a Java project.
$ mkdir myproject
$ cd myproject/
$ gradle init --type java-application --dsl groovy --test-framework junit-jupiter

Welcome to Gradle 7.4.2!

Here are the highlights of this release:
 - Aggregated test and JaCoCo reports
 - Marking additional test source directories as tests in IntelliJ
 - Support for Adoptium JDKs in Java toolchains

For more details see https://docs.gradle.org/7.4.2/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]

Project name (default: myproject):
Source package (default: myproject):

> Task :init
Get more help with your project: https://docs.gradle.org/7.4.2/samples/sample_building_java_applications.html

BUILD SUCCESSFUL in 19s
2 actionable tasks: 2 executed

# Add to Project Dependency

  1. Add the required configuration in build.gradle.
  2. Replace {VERSION} with a specific release version tag from releases (opens new window) without the leading v character.

NOTE

We recommended using the Java Server SDK version that matches your AccelByte Cloud version.

// build.gradle

repositories {
    ...
    maven {
        url 'https://nexus.accelbyte.net/repository/maven-releases/'
    }
}

dependencies {
    ...
    implementation 'net.accelbyte.sdk:sdk:{VERSION}'
}

# Use in Code

  1. Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in App.java. The DefaultConfigRepository gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.
// App.java

package myproject;

import net.accelbyte.sdk.api.iam.models.OauthmodelCountryLocationResponse;
import net.accelbyte.sdk.api.iam.operations.o_auth2_0_extension.GetCountryLocationV3;
import net.accelbyte.sdk.api.iam.wrappers.OAuth20Extension;
import net.accelbyte.sdk.core.AccelByteSDK;
import net.accelbyte.sdk.core.client.OkhttpClient;
import net.accelbyte.sdk.core.repository.DefaultConfigRepository;
import net.accelbyte.sdk.core.repository.DefaultTokenRepository;

public class App {
    public static void main(String[] args) throws Exception {
        // Create default HTTP client, token repository, and config repository instance

        OkhttpClient httpClient = new OkhttpClient();
        DefaultTokenRepository tokenRepository = new DefaultTokenRepository();
        DefaultConfigRepository configRepository = new DefaultConfigRepository();

        // Create SDK instance

        AccelByteSDK sdk = new AccelByteSDK(
                httpClient,
                tokenRepository,
                configRepository);

        // Login using client credentials

        boolean isLoginOk = sdk.loginClient();

        if (!isLoginOk) {
            System.exit(1); // Login failed
        }

        // Call an AccelByte Cloud endpoint e.g. GetCountryLocationV3

        OAuth20Extension wrapper = new OAuth20Extension(sdk);
        GetCountryLocationV3 operation = GetCountryLocationV3.builder()
                .build();
        OauthmodelCountryLocationResponse response = wrapper.getCountryLocationV3(operation);

        System.out.println(response.getCountryName());
    }
}

# Run the Code

  1. Set the required environment variables and run the code using gradle run.
$ export AB_BASE_URL="https://demo.accelbyte.io"              # AccelByte Cloud Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"      # AccelByte Cloud OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # AccelByte Cloud OAuth Client Secret
$ gradle run

# Import AccelByte Services

You can now start using the following AccelByte services in your app.

# SDK Examples

See our Java SDK example repo (opens new window)for a selection of test cases you can use to customize your game. You can also view the Java Server SDK README.md (opens new window) for more information.

IAM

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.iam.models.*;
import net.accelbyte.sdk.api.iam.operations.*;
import net.accelbyte.sdk.api.iam.wrappers.*;
Basic

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.basic.models.*;
import net.accelbyte.sdk.api.basic.operations.*;
import net.accelbyte.sdk.api.basic.wrappers.*;
Social

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.social.models.*;
import net.accelbyte.sdk.api.social.operations.*;
import net.accelbyte.sdk.api.social.wrappers.*;
Platform

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.platform.models.*;
import net.accelbyte.sdk.api.platform.operations.*;
import net.accelbyte.sdk.api.platform.wrappers.*;
Group

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.group.models.*;
import net.accelbyte.sdk.api.group.operations.*;
import net.accelbyte.sdk.api.group.wrappers.*;
Cloud Save

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.cloudsave.models.*;
import net.accelbyte.sdk.api.cloudsave.operations.*;
import net.accelbyte.sdk.api.cloudsave.wrappers.*;
DSM Controller

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.dsmc.models.*;
import net.accelbyte.sdk.api.dsmc.operations.*;
import net.accelbyte.sdk.api.dsmc.wrappers.*;
Session Browser

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.sessionbrowser.models.*;
import net.accelbyte.sdk.api.sessionbrowser.operations.*;
import net.accelbyte.sdk.api.sessionbrowser.wrappers.*;
Lobby

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.lobby.models.*;
import net.accelbyte.sdk.api.lobby.operations.*;
import net.accelbyte.sdk.api.lobby.wrappers.*;
Achievement

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.achievement.models.*;
import net.accelbyte.sdk.api.achievement.operations.*;
import net.accelbyte.sdk.api.achievement.wrappers.*;
DS Log Manager

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.dslogmanager.models.*;
import net.accelbyte.sdk.api.dslogmanager.operations.*;
import net.accelbyte.sdk.api.dslogmanager.wrappers.*;
UGC

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.ugc.models.*;
import net.accelbyte.sdk.api.ugc.operations.*;
import net.accelbyte.sdk.api.ugc.wrappers.*;
Leaderboard

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.leaderboard.models.*;
import net.accelbyte.sdk.api.leaderboard.operations.*;
import net.accelbyte.sdk.api.leaderboard.wrappers.*;
GDPR

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.gdpr.models.*;
import net.accelbyte.sdk.api.gdpr.operations.*;
import net.accelbyte.sdk.api.gdpr.wrappers.*;
Legal

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.legal.models.*;
import net.accelbyte.sdk.api.legal.operations.*;
import net.accelbyte.sdk.api.legal.wrappers.*;
Matchmaking

API Docs (opens new window)

SDK reference (opens new window)

import net.accelbyte.sdk.api.matchmaking.models.*;
import net.accelbyte.sdk.api.matchmaking.operations.*;
import net.accelbyte.sdk.api.matchmaking.wrappers.*;