All Products
Search
Document Center

Domain Names:SDK for Java code examples

Last Updated:Jun 10, 2026

Install and use the Alibaba Cloud Domain Names SDK for Java to call API operations such as submitting domain registration tasks.

Prerequisites

  1. A RAM user AccessKey pair is created. We recommend using a RAM user instead of an Alibaba Cloud account because a leaked account AccessKey exposes all resources. For information about how to obtain the AccessKey pair, see Create an AccessKey.

  2. The RAM user has domain name management permissions. This example uses the AliyunDomainFullAccess system policy.

    1. System policies:

      • AliyunDomainFullAccess: full permissions on Domain Names.

      • AliyunDomainReadonlyAccess: read-only access to domain registration and management.

    2. You can also use a custom policy.

      For more information about how to create a custom policy, see Create a custom policy.

  3. The RAM user AccessKey pair is configured in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.

Install the SDK

  1. Add a repository to your Maven configuration file.

    <repositories>
         <repository>
             <id>sonatype-nexus-staging</id>
             <name>Sonatype Nexus Staging</name>
             <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
         </repository>
    </repositories>
  2. Add the following dependency to your project's pom.xml <dependencies> section, then refresh Maven.

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>domain20180129</artifactId>
      <version>3.15.1</version>
    </dependency>
    

Use the SDK

1. Initialize a client

Alibaba Cloud SDKs support multiple credential types for client initialization, such as AccessKey pairs and STS tokens. For more information, see Manage credentials. This example uses an AccessKey pair.

import com.aliyun.domain20180129.Client;
import com.aliyun.teaopenapi.models.Config;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. 
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, visit https://api.aliyun.com/product/Domain.
                .setEndpoint("domain.aliyuncs.com");
        return new Client(config);
    }
}

2. Build a request object

Find the target API operation and its parameters in the API Reference, then build the request object.

Note

Request objects follow the {API name}Request naming convention. For example, SaveSingleTaskForCreatingOrderActivate uses SaveSingleTaskForCreatingOrderActivateRequest.

SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
                .setDomainName("example.com");

3. Call the operation

Note

Response objects follow the {API name}Response naming convention. For example, SaveSingleTaskForCreatingOrderActivate returns SaveSingleTaskForCreatingOrderActivateResponse.

// Specify the runtime parameters.
RuntimeOptions runtime = new RuntimeOptions();
SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);

4. Handle exceptions

The SDK for Java throws two exception types: TeaUnretryableException and TeaException.

  • TeaUnretryableException: thrown when network errors persist after all retry attempts are exhausted.

  • TeaException: thrown for business logic errors.

    Important

    This example prints errors for demonstration only. In production, handle exceptions properly — report errors, log details, and implement retries to ensure system stability.

try {
    Client client = createClient();
    SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
            .setDomainName("example.com");
    // Specify the runtime parameters.
    RuntimeOptions runtime = new RuntimeOptions();
    SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
} catch (TeaUnretryableException ue) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    ue.printStackTrace();
    // Obtain the error message.
    System.out.println(ue.getMessage());
    // Obtain the request message and query the request information when an error occurs.
    System.out.println(ue.getLastRequest());
} catch (TeaException e) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    e.printStackTrace();
    // Obtain the error code.
    System.out.println(e.getCode());
    // Obtain the error message that contains the request ID.
    System.out.println(e.getMessage());
    // Obtain the detailed error information that is returned by the server.
    System.out.println(e.getData());
} catch (Exception e) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    e.printStackTrace();
}

5. Complete example

import com.aliyun.domain20180129.Client;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateRequest;
import com.aliyun.domain20180129.models.SaveSingleTaskForCreatingOrderActivateResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class Sample {
    private static Client createClient() throws Exception {
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. 
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                // Specify an endpoint. For information about endpoints, visit https://api.aliyun.com/product/Domain.
                .setEndpoint("domain.aliyuncs.com");
        return new Client(config);
    }

    public static void main(String[] args_) {
        try {
            Client client = createClient();
            SaveSingleTaskForCreatingOrderActivateRequest request = new SaveSingleTaskForCreatingOrderActivateRequest()
                    .setDomainName("example.com");
            // Specify the runtime parameters.
            RuntimeOptions runtime = new RuntimeOptions();
            SaveSingleTaskForCreatingOrderActivateResponse response = client.saveSingleTaskForCreatingOrderActivateWithOptions(request, runtime);
        } catch (TeaUnretryableException ue) {
            // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
            ue.printStackTrace();
            // Obtain the error message.
            System.out.println(ue.getMessage());
            // Obtain the request message.
            System.out.println(ue.getLastRequest());
        } catch (TeaException e) {
            // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
            e.printStackTrace();
            // Obtain the error code.
            System.out.println(e.getCode());
            // Obtain the error message that contains the request ID.
            System.out.println(e.getMessage());
            // Obtain the detailed error information that is returned by the server.
            System.out.println(e.getData());
        } catch (Exception e) {
            // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
            e.printStackTrace();
        }
    }
}