Install the Java SDK
This topic describes how to install the Java SDK for managing Elastic Desktop Service (EDS) Enterprise cloud desktops, using IntelliJ IDEA on Windows as an example.
Prerequisites
The Java Development Kit (JDK) must be installed. For instructions, see Install the JDK on Windows.
IntelliJ IDEA must be installed. For instructions, see Set up a Java development environment on Windows.
Procedure
From OpenAPI explorer
-
Open the DescribeOfficeSites API reference and click Test in the upper-right corner.
-
On the OpenAPI Portal page, follow the prompts to sign in to your Alibaba Cloud account.
Enter the required parameters and click Call.
After a successful API call, click the SDK Sample tab in the right-side panel, select a language, and then click Download Sample Project.
NoteThe sample project contains the SDK and sample code for calling this API operation.
In IntelliJ IDEA, choose File > Open and select the unzipped sample project folder. Maven automatically installs the required dependencies.
Before you call an API operation, you must obtain an AccessKey pair. We recommend using the AccessKey pair of a RAM user. For more information, see Create an AccessKey pair.
ImportantAfter obtaining the AccessKey pair for a RAM user, configure it as environment variables. For detailed instructions, see Configure environment variables on Linux, macOS, and Windows.
Double-click Sample.java and click the Run icon in the upper-right corner.
View the result. In the log window at the bottom, press
Ctrl+Fand search forstatusCode. A"statusCode":200value indicates a successful API call.
In an existing project
Obtain the SDK.
Open the Elastic Desktop Service (EDS) Enterprise SDK installation page and choose an installation method. This topic uses Apache Maven as an example.
Add the SDK dependency.
In your Maven project, open the pom.xml file and add the following dependency to the
<dependencies></dependencies>element.<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>ecd20200930</artifactId> <version>4.3.0</version> </dependency> </dependencies>Download dependencies.
Right-click the project name and choose Maven > Reload project from the shortcut menu to download the dependencies.
Create a Java class.
Right-click the
src/main/javadirectory, choose New > Java Class, enterSampleas the class name, and press Enter.Initialize the cloud service client.
ImportantYou must provide an AccessKey pair for authentication to initialize the client. Obtain an AccessKey pair in advance. For more information, see Create an AccessKey pair.
After obtaining the AccessKey pair for a RAM user, you must also configure it as environment variables. For detailed instructions, see Configure environment variables on Linux, macOS, and Windows.
For more information about endpoints, see Endpoints.
import com.aliyun.ecd20200930.Client; import com.aliyun.teaopenapi.models.Config; public class Sample { public static void main(String[] args) throws Exception { Config config = new Config() .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")) .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); config.endpoint = "ecd.cn-shanghai.aliyuncs.com"; Client client = new Client(config); } }Call an API operation.
For each API operation, the SDK provides a request class and a response class. The request class defines the parameters and their data types, and the response class defines the structure of the returned data. This design helps prevent parameter spelling errors.
NoteRequest classes follow a naming convention of
<APIOperationName>Request. Example:DescribeOfficeSitesRequest.Response classes follow a naming convention of
<APIOperationName>Response. Example:DescribeOfficeSitesResponse.
import com.aliyun.ecd20200930.Client; import com.aliyun.ecd20200930.models.DescribeOfficeSitesRequest; import com.aliyun.ecd20200930.models.DescribeOfficeSitesResponse; import com.aliyun.teaopenapi.models.Config; public class Sample { public static void main(String[] args) throws Exception { Config config = new Config() .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")) .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); config.endpoint = "ecd.cn-shanghai.aliyuncs.com"; Client client = new Client(config); DescribeOfficeSitesRequest describeOfficeSitesRequest = new DescribeOfficeSitesRequest() .setRegionId("cn-shanghai"); DescribeOfficeSitesResponse describeOfficeSitesResponse = client.describeOfficeSites(describeOfficeSitesRequest); } }Handle exceptions.
The Alibaba Cloud SDK provides unified exception handling.
TeaException: This is the primary exception for errors returned by the service during an SDK request. This exception provides the following properties to help you troubleshoot issues:message: The error message of the API operation, which includes the RequestId.data: The error details returned by the server.
FAQ
Why do I get a NullPointerException on TeaException.getData()?
Cause: This error typically occurs when your Alibaba Cloud credentials are not configured correctly, for example, if the AccessKey pair is invalid or missing.
Incorrect example:
The error occurs because a string literal like "<YOUR_ACCESS_KEY_ID>" is incorrectly passed to the System.getenv() method. This method retrieves values from environment variables, not from string arguments.
Config config = new Config()
// System.getenv() retrieves the AccessKey ID and AccessKey secret from environment variables.
// Required. Make sure the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("<YOUR_ACCESS_KEY_ID>")) // Incorrect: Plaintext AccessKey ID
// Required. Make sure the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.setAccessKeySecret(System.getenv("<YOUR_ACCESS_KEY_SECRET>")); // Incorrect: Plaintext AccessKey secretCorrect example:
Set your AccessKey pair as environment variables, and then use the System.getenv() method to retrieve them.
Config config = new Config()
// System.getenv() retrieves the AccessKey ID and AccessKey secret from environment variables.
// Required. Make sure the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));Solution:
To avoid security risks, do not hard-code your AccessKey pair in your code.
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") and System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") retrieve the values of the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
Check whether the
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables are configured in your system.In a terminal (Linux/macOS) or Command Prompt (Windows), run the following commands. To open Command Prompt on Windows, click Start, click Run (or press
Win+R), typecmd, and then click OK. If the correct AccessKey pair is returned, the configuration is successful. If the output is empty or an error is reported, set the environment variables again. For more information, see Configure credentials.
Windows
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%Linux/macOS
echo $ALIBABA_CLOUD_ACCESS_KEY_ID
echo $ALIBABA_CLOUD_ACCESS_KEY_SECRETWhy do I get a "release version X not supported" error?
In IntelliJ IDEA, press Ctrl+Alt+Shift+S to open the Project Structure window. Select Modules. On the right, from the Language Level list, select the version that matches your JDK. For example, if you use JDK 8, select 8 - Lambdas, type annotations etc.. Click Apply and then OK.
Why do I get an "internal java compiler error"?
In the IntelliJ IDEA menu bar, choose File > Settings > Build, Execution, Deployment > Compiler > Java Compiler. Set Project bytecode version and Target bytecode version to match your JDK version. For example, if you use JDK 8, select 8 for both options. Click Apply and then OK.
Why do I get a "No SLF4J providers were found" error?
The "No SLF4J providers were found" error indicates that a project uses SLF4J but is missing a compatible logging implementation library. To resolve this issue, add the required dependency. For example, add the log4j-slf4j-impl dependency to the pom.xml file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.0</version>
</dependency>