# Install SDK for Java

## Prerequisites <a href="#title-6g8-ghg-nk5" id="title-6g8-ghg-nk5"></a>

Before you install and use Zenlayer Cloud SDK for Java, make sure that the following requirements are met:

* The Java environment is installed. Java Development Kit (JDK) version 1.7 or later is used.
* A Zenlayer Cloud account is created and an Access Key ID and an Access Key Password are created. See [Generate an API Access Key](https://docs.console.zenlayer.com/welcome/platform/team-management/generate-an-api-access-key) for more details.
* Obtain the service domain name. Currently the service domain name requested by Zenlayer Cloud API is `console.zenlayer.com`.

## Install SDK

### Method 1: (Recommended) Add dependencies to your Maven project

If you use Maven to manage Java projects, you can add Maven dependencies to the pom.xml file to install Zenlayer Cloud SDK for Java. In the [Maven repository](https://search.maven.org/search?q=zenlayercloud-sdk-java), you can view the Maven dependencies of Zenlayer Cloud services.

Add the following Maven dependency to install the core library of Zenlayer Cloud SDK for Java.

```
<dependency>
    <groupId>com.zenlayer</groupId>
    <artifactId>zenlayercloud-sdk-java</artifactId>
    <!-- Go to https://search.maven.org/search?q=zenlayercloud-sdk-java to check all new versions. The latest version: -->
    <version>0.6.0</version>
</dependency>
```

{% hint style="info" %} <mark style="color:blue;">**Note**</mark>

<mark style="color:blue;">The version that is used in the preceding dependency is a sample version. For information about the latest version of the core library, visit the</mark> [<mark style="color:purple;">Maven repository</mark>](https://search.maven.org/search?q=zenlayercloud-java-sdk)<mark style="color:blue;">.</mark>
{% endhint %}

### Method 2: Import the JAR package to your project

1. Go to [Github](https://github.com/zenlayer/zenlayercloud-sdk-java) to download Java *src.zip* file.
2. Unzip the source code file (*src.zip*) to a suitable location for your project.
3. Put the decompressed JAR file in the path where Java can open it.
4. Refer to the following Example.

## Example <a href="#title-6g8-ghg-nk5" id="title-6g8-ghg-nk5"></a>

Take `CreateInstances` as an example, you need to fill in the following parameters in the sample code according to your actual situation.

* \<AccessKey>: your Access Key ID. See [Obtain AccessKey](https://docs.console.zenlayer.com/api-reference/instruction/authorization/sign#applying-for-security-credentials) for more details.
* \<AccessSecret>: your Access Key Password. See [Obtain AccessKey ](https://docs.console.zenlayer.com/api-reference/instruction/authorization/sign#applying-for-security-credentials)for more details.

{% code lineNumbers="true" %}

```java
import com.zenlayercloud.bmc20221120.BmcClient;
import com.zenlayercloud.bmc20221120.models.request.CreateInstancesRequest;
import com.zenlayercloud.bmc20221120.models.response.CreateInstancesResponse;
import com.zenlayercloud.common.Credential;
import com.zenlayercloud.common.ZenlayerSdkException;

public class Example {

    public static void main(String[] args) {
        // 1. Initialize client
        Credential credential = new Credential("<AccessKey>", "<AccessSecret>");
        BmcClient client = new BmcClient(credential);
        
        // 2. Set API request parameters
        CreateInstancesRequest createInstancesRequest = new CreateInstancesRequest();
        createInstancesRequest.instanceChargeType = "PREPAID";
        createInstancesRequest.internetChargeType = "ByBandwidth";
        createInstancesRequest.zoneId = "SEL-A";
        createInstancesRequest.instanceTypeId = "M8C";

        try {
            // 3. Make an API request and get the response result
            CreateInstancesResponse instances = client.createInstances(createInstancesRequest);

        } catch (ZenlayerSdkException e) {
            // 4. Handle errors in response
        }
    }
    
}
```

{% endcode %}
