This topic describes how to install SDKs for Java and provides sample code.
Background information
- To use Resource Management SDK for Java, you must install both the core library of
Alibaba Cloud SDK for Java (
aliyun-java-sdk-core
) and Resource Management SDK for Java (aliyun-java-sdk-resourcemanager
). - Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
- For more information about Resource Management API operations, see List of operations by function.
Installation
You can use one of the following methods to install the SDKs for Java. For more information, see Quick start.
- Method 1: (Recommended) Add dependencies by using Maven
- 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
- 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>
NoteTake note of the following information about the packages:- The
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
aliyun-java-sdk-core
package of the latest version.
- The
- Use Maven to create a project.
- Method 2: Download the JAR files of the SDKs for Java and add them to your project
You can use this method to install the SDKs 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 example shows how to use Resource Management SDK for Java to call the CreateResourceAccount API operation. You can visit OpenAPI Explorer to debug other API operations and obtain their 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.resourcemanager.model.v20200331.*;
public class CreateResourceAccount {
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("cn-shanghai", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
// Construct the request object.
CreateResourceAccountRequest request = new CreateResourceAccountRequest();
// Configure parameters.
request.setRegionId("cn-shanghai");
request.setDisplayName("<DisplayName>");
// Initiate a 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());
}
}
}