Java SDK instructions

Last Updated: Dec 05, 2017

Introduction

Welcome to use Alibaba Cloud Development Kit (SDK). You can access a lot Alibaba Cloud services such as ECS and CloudMonitor by using Alibaba Cloud Java SDK without complex programming. This manual introduces how to get started with Alibaba Cloud Java SDK.

Environment requirements

  1. JDK 6 or later version is required.

  2. An Alibaba Cloud account and a pair of Access Key ID/Access Key Secret are required for Alibaba Cloud SDK. You can create and view your Access Keys in Access Keys Management Page, or contact your system administrator.

  3. In order to access APIs of a product using Alibaba Cloud SDK, you need to activate this product through Alibaba Cloud console first.

Download and install SDK

Click here to download Alibaba Cloud Java SDKs.

Note: SDKs of some Alibaba Cloud products (for example Object Storage Service, Table Store) are not included in this list. Please download these SDKs from detailed pages of respective products.

To make a valid API call to a product, you need to install both the core SDK and the product SDK. For example, a call to ECS will need both the core SDK and ECS SDK.

You can install SDKs by through the following two methods: adding dependencies into your Maven project, and importing JAR packages into your development environment (IDE).

If you are using Maven to manage your current Java project, you can install Java SDKs by adding the corresponding dependencies in your pom.xml. You can find the dependencies on the Alibaba Cloud Java SDK page.

The following example takes core SDK version 3.2.2 and ECS SDK version 3.0.0 as an example:

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>3.2.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-ecs</artifactId>
  9. <version>3.0.0</version>
  10. </dependency>

Method 2: Import JAR packages into your IDE

Whether you are using Eclipse or IntelliJ as your development environment, you can install Alibaba Cloud Java SDK by importing JAR packages into your IDE. You can find the JAR packages on the Alibaba Cloud Java SDK page.

Eclipse

  1. Download the aliyun-java-sdk-XXX.jar files to your project library folder.

  2. In Eclipse, click Project > Build Path > Configure Build Path.

  3. Under the Libraries tab, click Add Jars or Add External JARs.

  4. Select the downloaded JAR files in your project library folder.

IntelliJ

  1. Download the aliyun-java-sdk-XXX.jar files to your project library folder.

  2. In IntelliJ, click File > Project Structure > Modules.

  3. Under the Dependencies tab, click the green plus (+) button on right and then click JARs or directories.

  4. Select the downloaded JAR files in your project library folder.

Call Java SDKs

This following example shows the three steps required to call Alibaba Cloud SDKs:

  1. Create a DefaultAcsClient instance and initialize it.

  2. Create an API request and set parameters.

  3. Send the request, receive the response and handle exceptions.

You need to replace the following three parameters with your own values:

  • <your_region_id>: Refer to the Region list.
  • <your_access_key_id> and <your_access_key_secret>: View your Access Keys in the Access Key Management page, or contact your system administrator.
  1. import com.aliyuncs.profile.DefaultProfile;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.ecs.model.v20140526.*;
  7. class Sample {
  8. public static void main(String[] args) {
  9. // Create a DefaultAcsClient instance and initialize it.
  10. DefaultProfile profile = DefaultProfile.getProfile(
  11. "<your-region-id>", // Your region ID
  12. "<your-access-key-id>", // Your Access Key ID
  13. "<your-access-key-secret>"); // Your Access Key Secret
  14. IAcsClient client = new DefaultAcsClient(profile);
  15. // Create an API request and set parameters.
  16. DescribeInstancesRequest request = new DescribeInstancesRequest();
  17. request.setPageId(10);
  18. // Send the request, receive the response and handle exceptions.
  19. try {
  20. DescribeInstancesResponse response = client.getAcsResponse(request);
  21. } catch (ServerException e) {
  22. e.printStackTrace();
  23. } catch (ClientException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }

API reference

For APIs of more products, as well as their descriptions, parameters and return values, see the API list.

Thank you! We've received your feedback.