Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Setting up a Local Java Dev Environment

Setting up a development environment for Java can be daunting. Lets demystify it. To start with, you will need a few tools:

  • A Java distribution. This contains all the tools you need to turn Java source code into a program that you can run on your computer. You can’t develop Java applications without this.
  • A build tool. Build tools help you add other libraries to your project and can build and run your application. You could do all that by hand, but a build tool will save you so much time and confusion that we don’t recommend developing without one.
  • An IDE. You can write Java code in any text editor, but a good IDE will provide a lot of help - syntax highlighting, refactoring help, integration with your build tool and more.

Once you have these installed it’s time to create Twilio applications in Java.

Installing the required tools

An excellent way to install the Java distribution and build tool is with SDKMAN! After you have installed SDKMAN! you can use the sdk command on the terminal to install different tools and different versions.

Installing a Java distribution

In a terminal, you can see what versions of Java are available to install using the command sdk list java - there are a lot, and if you don’t know which you want then we recommend picking the latest HotSpot version from AdoptOpenJDK. New versions are released frequently. At the moment the latest is 14.0.1.hs-adpt (hs is short for HotSpot, adpt for AdoptOpenJDK), so install with sdk install java 14.0.1.hs-adpt. This will download the Java distribution and set up any necessary environment variables to use it.

If you are not using SDKMAN! you can download it directly from AdoptOpenJDK, but you will need to set the JAVA_HOME environment variable yourself as well as making sure that the java command is on your PATH.

You can check if everything is working correctly by running java -version in a terminal. You should see output like this:

openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)

Installing a build tool

The two most popular build tools are Apache Maven and Gradle. Maven is more widely used so if you are not sure which to choose, we recommend installing Maven.

To install Maven with SDKMAN! type the following command sdk install maven 3.6.3. Without SDKMAN! you will need to download and install Maven yourself.

Check if Maven is installed correctly with mvn --version which should print something like the this:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/twilio/.sdkman/candidates/maven/current
Java version: 14.0.1, vendor: AdoptOpenJDK, runtime: /home/twilio/.sdkman/candidates/java/14.0.1.hs-adpt
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "5.3.0-53-generic", arch: "amd64", family: "unix"

Installing an IDE

The most popular IDE for Java is Intellij IDEA. The community edition is free and very powerful. There are other IDEs - if you are familiar with Eclipse or Netbeans then feel free to use them too.

After downloading and installing an IDE, you are ready to create Java applications.

Creating a Java project

Using your IDE you can create a new project and then select Maven or Gradle as the build tool. This will create the correct directory structure and open the project. For either build tool, your application’s source code will go in src/main/java.

To check that everything is working correctly, create a file called HelloTwilio.java in src/main/java with the following contents:

public class HelloTwilio {
   public static void main(String[] args) {
       System.out.println("Hello Twilio");
   }
}

You will be able to run that code within the IDE. Intellij has a small “play” button next to the method, and other IDEs are similar.

To learn more about programming in Java, there are many good free resources online. Here are a few:

If you get stuck you can search or ask questions on places such as Stackoverflow or r/learnjava and r/javahelp on Reddit.

Creating Twilio applications in Java

Your applications can interact with Twilio in two ways:

  • calling the Twilio API from your code
  • having Twilio call your application using a webhook.

Many applications will do both.

Calling the Twilio API from your application

To do this you will need to add the Twilio Java helper library to your project. Because you have set up a build tool this is done by adding a "dependency" to the build configuration.

Adding a dependency on the Twilio helper library

For Maven, in pom.xml in the <dependencies> section, add:

<dependency>
    <groupId>com.twilio.sdk</groupId>
    <artifactId>twilio</artifactId>
    <version>7.50.1</version>
</dependency>

For Gradle, in build.gradle in the dependencies{} section add:

implementation group: 'com.twilio.sdk', name: 'twilio', version: '7.50.1'

Using the Twilio helper library

With the helper library added to your project you can now use the classes it contains. For example the Message class which can be used to send SMS messages from Java code.

Handling webhook calls from Twilio in Java

To handle a webhook you will need to run a web server in your code. Java has several options for this, but by far the most popular is Spring Boot. Spring Boot projects can be created using the Spring Initializr, or with the Spring CLI tool (which you can install with sdk install springboot).

If you are using the Spring CLI, create a new Spring Boot project with the following command:

spring init \         
  --dependencies web \
  --groupId com.example \
  --artifactId my-first-twilio-project \
  --extract

If you are using the online Spring Initializr, enter the same details into the website and download and extract the project that it creates.

To handle incoming HTTP requests, create a class annotated with @Controller. Here’s an example of a Controller which can be used to respond to an SMS message:

package com.example.myfirsttwilioproject;

import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.messaging.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class WebhookController {

   @PostMapping("/hook")
   @ResponseBody
   public String getTwiML() {

       return new MessagingResponse.Builder().message(
           new Message.Builder("Thanks for your message").build())
           .build()
           .toXml();

   }
}

To learn more about responding to web requests with Spring Boot, there are many good resources online:

Serving content online

When you develop a web application on your laptop it will be available for you to make HTTP requests by referring to localhost, but for Twilio to be able to reach it you will need a public URL. A good tool that we use a lot to test webhooks is ngrok - this video talks more about what it does and why we like it.

Congratulations

You now have everything you need to start using Twilio and Java! See where your coding inspiration takes you next with our Java quickstarts and the Java content on our blog.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.