This topic describes how to install SDK for Java and provides an example of how to use SDK for Java.

Background information

To use SDK for Java, you must install the core library of Alibaba Cloud SDK for Java and Resource Management SDK for Java.

  • The name of the core library is aliyun-java-sdk-core. You can use OpenAPI Explorer to generate sample code and perform debugging.
  • The name of Resource Management SDK for Java is aliyun-java-sdk-resourcemanager. For more information about Resource Management API operations, see API Reference.

Install SDK for Java

Add dependencies by using Maven. Alternatively, download the JAR packages of SDK for Java and add them to your project.

  • (Recommended) Add dependencies by using Maven
    1. Use Maven to create a project.
      mvn archetype:generate -DgroupId=com.aliyun.resourcemanager.sample \
      -DartifactId=resourcemanager-sdk-sample \
      -Dpackage=com.aliyun.resourcemanager.sample \
      -Dversion=1.0-SNAPSHOT
    2. Add dependencies to the pom.xml file of the project.

      The following code is an example of the dependencies on the aliyun-java-sdk packages. In this example, the SDK version is 2.0.7.

      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-resourcemanager</artifactId>
          <version>1.0.0</version>
      </dependency>
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.4.6</version>
      </dependency>
      Note
      Note the following information of the packages:
      • aliyun-java-sdk packages have been added to the Maven repository. You do not need to edit the settings.xml file.
      • You can visit the Maven repository to obtain the latest version of the aliyun-java-sdk-core package.
  • Download the JAR packages of SDK for Java and add them to your project

    You can download the JAR packages from the following links:

Use SDK for Java

The following example demonstrates how to use SDK for Java to create a resource account:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.resourcemanager.model.v20200331.*;
public class CreateResourceAccount {
    public static void main(String[] args) {
        // Construct an Alibaba Cloud client to send requests.
        // When you construct the client, specify the AccessKey ID and AccessKey secret.
        // Resource Management is a global service. However, the endpoint of the Resource Management API is located in the China (Shanghai) region. Therefore, set the region to cn-shanghai.
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        // Construct the request object.
        CreateResourceAccountRequest request = new CreateResourceAccountRequest();
        // Specify the required parameters.
        request.setRegionId("cn-shanghai");
        request.setDisplayName("test");
        // Issue the request and obtain a response.
        try {
            CreateResourceAccountResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch(ServerException e) {
            e.printStackTrace();
        } catch(ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}