Using the Watson Java SDK

IBM's Watson Computing System

The era of cognitive computing is here, and IBM Watson will be bringing new capabilities to IBM software in the near future. As a developer, you also have the ability to embed cognitive capabilities into your own applications. One of the ways you can do this is by using the Watson Java SDK. This SDK provides wrappers around the underlying Watson services and makes the APIs quite easy to use. Here’s how you can get started:

  1. Download the latest release of the Watson Java SDK. This SDK includes all of the required dependencies, including the Google GSON library for parsing the Watson and Bluemix JSON responses.
  2. Add the JAR to your project’s classpath
  3. Review the Watson API Documentation to see what services are available and how to use them.  For more in depth information, review the “API Reference” link on the page for any given service.
  4. Review the Watson API Explorer for more details about specific services and to try them out!

As an example, here’s a code snippet showing how easy it can be to use the Watson Language Translation service.

First, you need to create your application and add one of the Watson services in Bluemix.  Next, you will want to obtain the service credentials.  If you need to obtain the credentials programmatically, and your application is running in Bluemix, you can get them from the environment variables using the GSON library using the code sample below.  After you have the credentials, using the Watson Language Translation service is pretty easy!  The following code will translate “Hello” from English to Spanish.

JsonObject vcap = new JsonParser().parse(System.getenv("VCAP_SERVICES")).getAsJsonObject();
JsonObject language = vcap.getAsJsonArray("language_translation").get(0).getAsJsonObject();
JsonObject credentials = language.getAsJsonObject("credentials");
String username = credentials.get("username").getAsString();
String password = credentials.get("password").getAsString();

LanguageTranslation service = new LanguageTranslation();
service.setUsernameAndPassword(username, password);

TranslationResult translationResult = service.translate("Hello", "en", "es");

That’s it! You can start embedding cognitive capabilities into your Java applications today!

You may also like...

Leave a Reply