This topic describes how to install Resource Access Management (RAM) SDK for Java and provides an example on how to use RAM SDK for Java.

Background information

  • To use RAM SDK for Java, you must install the core library of Alibaba Cloud SDK for Java (aliyun-java-sdk-core) and the core library of Alibaba Cloud RAM SDK for Java (aliyun-java-sdk-ram).
  • Alibaba Cloud supports OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For information about RAM API operations, see List of operations by function.

Install RAM SDK for Java

You can use one of the following methods to install RAM SDK for Java. For more information, see Quick start.

  • Method 1: (Recommended) Add dependencies by using Maven
    1. Use Maven to create a project.
      mvn archetype:generate -DgroupId=com.aliyun.ram.sample \
      -DartifactId=ram-sdk-sample \
      -Dpackage=com.aliyun.ram.sample \
      -Dversion=1.0-SNAPSHOT
    2. Add the following dependencies to the pom.xml file of the project:
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-ram</artifactId>
          <version>3.2.0</version>
      </dependency>
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.5.18</version>
      </dependency>
      Note You can visit the following links to obtain the latest versions of the aliyun-java-sdk-core and aliyun-java-sdk-ram packages.
  • Method 2: Download the JAR files of RAM SDK for Java and add these JAR files to your project

    You can use this method to install RAM SDK for Java regardless of whether you are using Eclipse or IntelliJ as the integrated development environment (IDE). You can download the JAR files from the following links:

Example

The following sample code provides an example on how to call the CreateUser operation by using the SDK for Java. For other operations, you can use OpenAPI Explorer to debug the operations and obtain sample code.

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.ram.model.v20150501.*;

public class CreateUser {

    public static void main(String[] args) {
        
        // Construct an Alibaba Cloud client that is used to initiate requests. 
        // When you construct the client, specify your AccessKey ID and AccessKey secret. 
        DefaultProfile profile = DefaultProfile.getProfile("<accessKeyId>", "<accessKeySecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        
        // Construct a request and specify request parameters. For more information about the parameters, see API Reference. 
        CreateUserRequest request = new CreateUserRequest();
        request.setUserName("<UserName>");
        
        // Initiate the request and obtain a response. 
        try {
            CreateUserResponse 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());
        }

    }
}