edit-icon download-icon

Use Java SDK

Last Updated: Mar 20, 2018

Alibaba Cloud Java SDK supports JDK 1.6 or later. You can install the Java SDK using the following two methods:

  • Use Maven (recommended)

    If you use Maven to manage project dependencies, add the following codes to pom.xml to install Java SDK:

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-core</artifactId>
    4. <version>3.5.0</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>
  • Import JARs to the integrated development environment

    If you are using Eclipse or IntelliJ, you can install the Java SDK directly by importing JAR files.

    Note: This installation method will be eliminated in the next major version, and will only support the Maven installation method.

    Eclipse

    Complete the following steps to install Alibaba Cloud Java SDK:

    1. Copy the downloaded aliyun-java-sdk-XXX.jar files to your project folder.

    2. Open your project in Eclipse, right click the project, and select Properties.

    3. In the displayed dialog box, click Java Build Path. Then click Add JARs in the Libraries tab to add the downloaded JAR files.

      Eclipse

    4. Click Apply and Close.

      IntelliJ

      Complete these steps to install Alibaba Cloud Java SDK:

    5. Copy the downloaded aliyun-java-sdk-XXX.jar files to your project folder.

    6. Open your project in IntelliJ, and click File > Project Structure in the menu bar.

    7. In the displayed dialog box, click Modules and then click the Dependencies tab. In the displayed list, click the add button to add the JAR files.

      IntelliJ

    8. Click Apply and then click OK.

Set up credentials

When using the Alibaba Cloud Java SDK to access Alibaba Cloud services, you have to provide your identification for authentication.

Java SDK supports the following authentication methods:

Authentications Descriptions
AccessKey Use the AccessKeyID/Secret to do the authentication
StsToken Use the STS Token to do the authentication
RamRoleArn Use the AssumeRole of the RAM account to do the authentication
EcsRamRole Use the RAM role of an ECS instance to do the authentication
RsaKeyPair Use the RSA key pair to do the authentication (supported only on Japan site)

This document uses AccessKey as an example to illustrate how to set up credentials.

Note: To ensure the security of your account, we recommend using your RAM account instead of the primary account. The primary account has full access to all of your cloud services, while the RAM account has limited access granted by the primary account to the cloud services.

Firstly, create an AccessKey as described in Create an AccessKey, and then set up your credentials when initializing AcsClient as follows:

Note: Do not disclose any code containing your AccessKey (do not commit the code to public GitHub projects). Otherwise, your Alibaba Cloud account may be compromised.

  1. DefaultProfile profile = DefaultProfile.getProfile(
  2. "<your-region-id>", // Your region ID
  3. "<your-access-key-id>", // The AccessKey ID of your RAM account
  4. "<your-access-key-secret>"); // The AccessKey Secret of your RAM account

Call a service

This document uses ECS as an example to illustrate how to use Alibaba Cloud Java SDK to make a request:

  1. Create an AcsClient client.

    1. IAcsClient client = new DefaultAcsClient(profile);
  2. Create a request.

    The naming convention for requests is ${apiName}Request.

    • Where ${apiName} is the API name, such as DescribeInstances.
    • When multiple product SDKs are used, different requests may have the same name. Differentiate the request according to the package.

      1. DescribeInstancesRequest request = new DescribeInstancesRequest();
      2. request.setPageSize(10);
  3. Make a call and handle the response.

    1. DescribeInstancesResponse response;
    2. try {
    3. response = client.getAcsResponse(request);
    4. for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
    5. System.out.println(instance.getPublicIpAddress());
    6. }
    7. } catch (ServerException e) {
    8. e.printStackTrace();
    9. } catch (ClientException e) {
    10. e.printStackTrace();
    11. }

    All the returned attributes are deserialized into the response. You can directly call response.xxx to obtain the response attributes.

    1. instanceStatus := response.getStatus()

    However, if an exception occurs or you want to obtain the original HTTP response, you can use the doAction() method to obtain the original response.

    1. HttpResponse response = client.doAction(request);
Thank you! We've received your feedback.