2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

Use the Watson Developer Java SDK to call the Watson NLU service

Devices that run Java are everywhere these days, and IBM has a Java SDK that lets you integrate and use IBM Watson services on a Java device.

Learning objectives

This guide is for new developers who want to understand more about the NLU REST API call and how you can use it inside your Java code to call a microservice.


Before beginning this tutorial, you’ll need:

  • An IBM Cloud account: If you do not have an IBM Cloud account, you can create one.
  • A Java IDE: I used Eclipse.

Estimated time

Completing this how-to should take approximately 15 minutes.


Create the Java project

  1. Download the Watson Java SDK. You can get more information about the Watson SDK on the watson-developer-cloud/java-sdk site.

  2. Open your IDE, and open new project.


  3. Name the project, and click Finish (make sure you’re using Java version 1.8 or higher).


  4. Right-click on the new project and create a new class.


  5. Name the class. Make sure you check the public static void main(String[] args).


  6. Insert the Watson SDK .jar file that you downloaded previously and use Watson premade classes by right-clicking on the project and going to Configure Build Path….


  7. Select Add External JARs….


  8. Select the .jar file you downloaded.


  9. Click OK, and you’re done. Now you can insert some sample code. Copy and paste the following code to your class.

     //Import gives you access to the classes inside the jar file you used.
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.NaturalLanguageUnderstanding;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EntitiesOptions;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.KeywordsOptions;
     public class Main {
       public static void main(String[] args) {
         //Creating the class of the service. Make sure to insert you service username and password.
         NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(
         //The text we want to analyze. You can insert any other text you like.
         String text = "IBM is an American multinational technology " +
           "company headquartered in Armonk, New York, " +
           "United States, with operations in over 170 countries.";
         //Entities and keywords are parameters you get back from the service about your text.
         EntitiesOptions entitiesOptions = new EntitiesOptions.Builder()
         KeywordsOptions keywordsOptions = new KeywordsOptions.Builder()
         Features features = new Features.Builder()
         AnalyzeOptions parameters = new AnalyzeOptions.Builder()
         //Take the parameters and send them to your service for resutls.
         AnalysisResults response = service
         //print the result

Create the NLU service

To create the NLU service:

  1. Go to the NLU Service and click Create. If you get errors about missing spaces, try to change the regions. If you don’t have any space, you need to create new space.


Input credentials

  1. After you create the service, select Service credentials from the left menu.


  2. Click New credential to create a username and password, and then click Add.


  3. Click View credentials and copy the username and password to the code.



Test the application



Now that you’re familiar with the Waston Java SDK, you can use other Watson services. Try to call another Watson service: Language Translator. Natural Language Classifier. Personality Insights. Text To Speech. Visual Recognition. Tone Analyzer.