All Products
Search
Document Center

Java SDK

Last Updated: May 28, 2019

Download URL

The Machine Translation Java SDK allows you to operate Alibaba Cloud Machine Translation conveniently by using Java programs. You can directly use Maven dependencies to add the SDK or download the package to your local machine. Currently, the SDK supports J2SE 6.0 and later. Click Here to download the Machine Translation SDK of the latest version, and click Here to download the Alibaba Cloud SDK of the latest version.

Procedure

To quickly start using the Machine Translation Java SDK, take the following steps:

Step 1. Create an Alibaba Cloud account

For more information about how to create an Alibaba Cloud account, see Sign up with Alibaba Cloud.

We recommend that you complete real-name authentication as soon as possible to better use Alibaba Cloud services. Otherwise, some services may be unavailable.

Step 2. Obtain an Alibaba Cloud Access Key

You must apply for an Alibaba Cloud ACCESS KEY for using the Machine Translation Java SDK.

Log on to Alibaba Cloud’s Access Key Management page. Select an Access Key (consisting of an Access Key ID and Access Key Secret) for the SDK. If you do not have an Access Key, create one and make sure the Access Key is enabled.

The Access Key will be used in the following steps and must be kept confidential.

Step 3. Install the Javadevelopment environment

Currently, the Machine Translation Java SDK supports the Java runtime environment of J2SE 6.0 and later. You can download the installation package from the Java website and install the Java development environment based on instructions.

Step 4. Install the Machine TranslationJava SDK

Install the Machine Translation Java SDK after the Java development environment is installed. Currently, two installation methods are available.

  1. We recommend that you use Apache Maven to obtain the latest SDK version. You can add the following configurations to your Maven project:

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-core</artifactId>
    4. <version>4.3.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>aliyun-java-sdk-alimt</artifactId>
    9. <version>1.0.3</version>
    10. </dependency>
  2. You can download the Java SDK package and directly reference the local package in your Java project.

  • Click Here to clone the Java SDK package. You also need to download the Alibaba Cloud SDK of the latest version. The version is updated periodically. To use the latest version, use Maven.

  • Extract the downloaded package to a specified directory. The Java SDK does not require installation.

  • Add all .jar packages (including third-party dependent packages) in the SDK package to your Java project. For detailed instructions, see the corresponding IDE document.

Step 6. Start a new Java project

Now, you can start using the SDK. Use any text editor or the Java IDE to run the following sample code, and you can interact with the Machine Translation server and obtain the relevant output. For more information about using the Java SDK, see Instructions in this document.

Java sample code

  1. package com.cloud;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.alimt.model.v20181012.TranslateECommerceRequest;
  6. import com.aliyuncs.alimt.model.v20181012.TranslateECommerceResponse;
  7. import com.aliyuncs.http.MethodType;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. import java.net.URLEncoder;
  10. public class TranslateTest {
  11. public static void main(String[] args) {
  12. String accessKeyId = "<your_access_key_id>";// AccessKeyId
  13. String accessKeySecret = "<your_access_key_secret>"; // accessKeySecret
  14. // Create a DefaultAcsClient instance and initialize
  15. try {
  16. DefaultProfile profile = DefaultProfile.getProfile(
  17. "cn-hangzhou", // region
  18. accessKeyId, // Alibaba Cloud Account AccessKey ID
  19. accessKeySecret); // Alibaba Cloud Account Access Key Secret
  20. IAcsClient client = new DefaultAcsClient(profile);
  21. // Create API requests and set parameters
  22. TranslateECommerceRequest eCommerceRequest = new TranslateECommerceRequest();
  23. eCommerceRequest.setScene("title");
  24. eCommerceRequest.setMethod(MethodType.POST); // Set request method,POST
  25. eCommerceRequest.setFormatType("text"); // Format of translated text
  26. eCommerceRequest.setSourceLanguage("zh"); // Source language
  27. eCommerceRequest.setSourceText(URLEncoder.encode("你好","UTF-8")); // Source text
  28. eCommerceRequest.setTargetLanguage("en"); // Target language
  29. TranslateECommerceResponse eCommerceResponse = client.getAcsResponse(eCommerceRequest);
  30. System.out.println(JSONObject.toJSON(eCommerceResponse));
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }