This topic describes how to install the environment for using Alibaba Cloud Darabonba SDK for Java and run the sample code to call an API operation.
Preparations
Prerequisites
Java Development Kit (JDK) V1.8 or later is downloaded and installed. To download the required JDK version, go to the Java Downloads page on the Oracle official website.
An integrated development environment (IDE) is downloaded and installed. In this example, IntelliJ IDEA is used. To download IntelliJ IDEA, visit the IntelliJ IDEA official website.
An AccessKey pair is created. When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create an AccessKey pair, see Create an AccessKey pair.
Java version
Run the java -version
command on the terminal to check the Java version.
Run the sample code
Step 1: Download an SDK project
Log on to OpenAPI Explorer.
In the top navigation bar, click Select a cloud service. In the panel that appears, select the cloud service that you want to manage. The page of the selected cloud service appears.
In the top navigation bar, click API Debugging. Then, a page with three columns appears.
Select the API operation that you want to call in the leftmost column. Configure the required parameters in the middle column. Then, click the SDK Sample Code tab in the rightmost column.
Select V2.0 from the SDK Version drop-down list. Select a programming language based on your business requirements.
Click Download Project to download the sample code.
If you want to generate generic code, turn on Common Request. For more information, see Generic calls and specialized calls.
Step 2: Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
If you use a Linux or macOS operating system, perform the following operations:
Create the
.bash_profile
file.NoteIf you configure environment variables for the first time, you can run the
touch ~/.bash_profile
command to create the configuration file. If the configuration file already exists, run thevim ~/.bash_profile
command.touch .bash_profile vim ~/.bash_profile
Configure the
.bash_profile
file.# Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
Save the configuration and close the file.
Use the
.bash_profile
file.# Run the commands in the configuration file. source ~/.bash_profile # Check whether the configuration takes effect. echo $ALIBABA_CLOUD_ACCESS_KEY_ID
If you use a Windows operating system, create a file to add the
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables, and set the variables to your AccessKey ID and AccessKey secret.
Restart the IDE. Otherwise, the environment variables may fail to be read.
If you use a Linux operating system, the preceding export commands serve only as temporary environment variables and take effect only for the program that is run in this step. For example, you can run the mvn package
command to package a Maven program into a JAR file, and then run the java -jar <jarfilepath>
command to run the JAR file.
Step 3: Run the code
Run the code in IntelliJ IDEA. After the code is run, view the returned information and
request ID
in the body of console logs.If you call an API operation to create an instance, the instance ID is also returned. You can log on to the console of the Alibaba Cloud service to verify the returned information based on the instance ID.