All Products
Search
Document Center

Machine Translation:Java SDK

Last Updated:Nov 20, 2025

This topic describes how to use Machine Translation SDK for Java. You will learn how to:

  • Configure an account

  • Set up the Java environment

  • Use the Java SDK

Procedure

Step 1: Activate the service and get a key

  1. Register an account: If you do not have an Alibaba Cloud account, first register.

  2. Activate Machine Translation service: Go to the Machine Translation Service page, select the service that you want to activate, and click Open now.

  3. Get an AccessKey: Go to the AccessKey page and get the AccessKey ID and AccessKey secret.

Step 2: Set up the Java environment

  1. Check your Java version

    To use Machine Translation SDK, you need Java 8 or later. Run the following command in the terminal to check the Java version:

    java -version

    To confirm the Java version, check the first line of the returned message. For example, openjdk version "16.0.1" 2021-04-20 message indicates that the current version is Java 16. If your current computing environment does not have Java or is earlier than Java 8, go to Java Download to download and install Java.

  2. Add dependencies

    Add the following code to the pom.xml dependency file and replace the-latest-version with the latest version number.

    <!-- https://mvnrepository.com/artifact/com.aliyun/alimt20181012 -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>alimt20181012</artifactId>
      <version>the-latest-version</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.aliyun/tea-openapi -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>the-latest-version</version>
    </dependency>

Step 3: Call the Machine Translation SDK

Use the following code to call the Machine Translation SDK, taking TranslateGeneral as an example..

Important

Replace the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET in the code with the AccessKey ID and AccessKey secret that you got in Step 1.

package com.aliyun.sample;

import com.aliyun.tea.*;
import com.aliyun.alimt20181012.*;
import com.aliyun.alimt20181012.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.darabonba.env.*;
import com.aliyun.teaconsole.*;

public class Sample {
    public static com.aliyun.alimt20181012.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Replace ALIBABA_CLOUD_ACCESS_KEY_ID with the AccessKey ID. 
                .setAccessKeyId ("ALIBABA_CLOUD_ACCESS_KEY_ID")
                // Replace ALIBABA_CLOUD_ACCESS_KEY_SECRET with the AccessKey secret. 
                .setAccessKeySecret ("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // For endpoint info, see https://api.aliyun.com/product/alimt.
        config.endpoint = "mt.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.alimt20181012.Client(config);
    }
    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.alimt20181012.Client client = Sample.createClient();
        TranslateGeneralRequest request = new TranslateGeneralRequest()
                .setFormatType("text")
                .setSourceLanguage("zh")
                .setTargetLanguage("en")
                . setSourceText("你好")
                .setScene("general");
        TranslateGeneralResponse response = client.translateGeneral(request);
        com.aliyun.teaconsole.Client.log(response.body.data.translated);
    }
 }

Output:

db3d9b398c65821100d85b314b65e40c

Go to Machine Translation API for more API examples.