This topic describes how to install SDK for Java and provides examples 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 (aliyun-java-sdk-core) and Tag SDK for Java (aliyun-java-sdk-tag).
  • The JDK version must be 1.6 or later.
  • 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 information about Tag API operations, see Tag management.

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.tag.sample \
      -DartifactId=tag-sdk-sample \
      -Dpackage=com.aliyun.tag.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-core</artifactId>
          <version>4.3.0</version>
      </dependency>
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-tag</artifactId>
          <version>1.0.1</version>
      </dependency>
      Note 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

    If you are using Eclipse or IntelliJ as the IDE, you can use this method to install SDK for Java. You can download the JAR packages of SDK for Java from the following links:

Use SDK for Java

The following examples provide the sample code of only some API operations. For information about the sample code of other API operations, visit OpenAPI Explorer. Then, find the target API operation, debug the operation, and obtain sample code.

  • Create and bind a tag
    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.tag.model.v20180828.*;
    
    public class TagResources {
    
        public static void main(String[] args) {
            // Construct an Alibaba Cloud client to send requests.
            // When you construct the client, specify the region ID, AccessKey ID, and AccessKey secret.
            DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
            
            // Construct the request object.
            TagResourcesRequest request = new TagResourcesRequest();
            
            // Specify the required parameters.
            request.setRegionId("cn-qingdao");
            request.setTags("{<tagKey>:<tagValue>}");
            List<String> resourceARNList = new ArrayList<String>();
            resourceARNList.add("<resourceARN>");
            request.setResourceARNs(resourceARNList);
    
            // Issue the request and obtain a response.
            try {
                TagResourcesResponse 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());
            }
    
        }
    }
  • Query tags
    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.tag.model.v20180828.*;
    
    public class ListTagResources {
    
        public static void main(String[] args) {
            // Construct an Alibaba Cloud client to send requests.
            // When you construct the client, specify the region ID, AccessKey ID, and AccessKey secret.
            DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
            
            // Construct the request object.
            ListTagResourcesRequest request = new ListTagResourcesRequest();
            
            // Specify the required parameters. You can specify the Alibaba Cloud Resource Names (ARNs) of different types of resources.        
            request.setRegionId("cn-qingdao");
            List<String> resourceARNList = new ArrayList<String>();
            resourceARNList.add("arn:acs:ecs:cn-qingdao:<AccountId>:instance/<ResourceId>");
            resourceARNList.add("arn:acs:vpc:cn-qingdao:<AccountId>:vpc/<ResourceId>");
            request.setResourceARNs(resourceARNList);
            
            // Issue the request and obtain a response.
            try {
                ListTagResourcesResponse 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());
            }
    
        }
    }