Setting up a development environment for Java can be daunting, so let's demystify it.
To start with, you will need a few tools:
Once you have these installed, you can then create Twilio applications in Java.
An excellent way to install the Java distribution and build tool is with SDKMAN!. After you have installed SDKMAN! — see the above link — you can use the sdk
command to install different tools and different versions of the SDK and Java.
In a terminal, you can see what versions of Java are available to install using the command sdk list java
— there are many, 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 15.0.1.hs-adpt
(hs
is short for HotSpot, adpt
for AdoptOpenJDK), so install with:
sdk install java 15.0.1.hs-adpt
This will download the Java distribution and set up any necessary environment variables to use it.
If you aren't using SDKMAN!, you can download Java 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:
1openjdk version "15.0.1" 2020-10-202OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.1+9)3OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15.0.1+9, mixed mode, sharing)
The two most popular build tools are Maven and Gradle. Maven is more widely used so if you are not sure which to choose, we recommend installing that.
To install Maven with SDKMAN!, type the following command:
sdk install maven 3.6.3
Without SDKMAN!, you will need to download and install it yourself.
Check if Maven is installed correctly with mvn --version
. It should print something like this:
1Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)2Maven home: /home/twilio/.sdkman/candidates/maven/current3Java version: 15.0.1, vendor: AdoptOpenJDK, runtime: /home/twilio/.sdkman/candidates/java/15.0.1.hs-adpt4Default locale: en_GB, platform encoding: UTF-85OS name: "linux", version: "5.3.0-53-generic", arch: "amd64", family: "unix"
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. There is also good Java support in Microsoft Visual Studio Code.
After downloading and installing an IDE, you are ready to create Java applications.
With your IDE, 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:
1public class HelloTwilio {2public static void main(String[] args) {3System.out.println("Hello Twilio");4}5}
You will be able to run that code within the IDE. IntelliJ has a small Play button, and other IDEs are similar.
To learn more about programming in Java, check out some of the 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.
Your applications can interact with Twilio in two ways:
Many applications will do both.
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.
For Maven, locate the file pom.xml
in the <dependencies>
section, then add:
1<dependency>2<groupId>com.twilio.sdk</groupId>3<artifactId>twilio</artifactId>4<version>8.0.0</version>5</dependency>
For Gradle, locate the build.gradle
file in the dependencies{}
section, then add:
implementation group: 'com.twilio.sdk', name: 'twilio', version: '8.0.0'
With the helper library added to your project, you can now use the classes it contains, such as the Message
class, which can be used to send SMS messages.
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 online 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:
1spring init \2--dependencies web \3--groupId com.example \4--artifactId my-first-twilio-project \5--extract
If you are using the online Spring Initializr, enter the same details into the website when requested to do so, 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:
1package com.example.myfirsttwilioproject;23import com.twilio.twiml.MessagingResponse;4import com.twilio.twiml.messaging.Message;5import org.springframework.stereotype.Controller;6import org.springframework.web.bind.annotation.PostMapping;7import org.springframework.web.bind.annotation.ResponseBody;89@Controller10public class WebhookController {1112@PostMapping("/hook")13@ResponseBody14public String getTwiML() {1516return new MessagingResponse.Builder().message(17new Message.Builder("Thanks for your message").build())18.build()19.toXml();20}21}
To learn more about responding to web requests with Spring Boot, you can use some good online resources:
When you develop a web application on your computer, it will be available to you via localhost
. However, for Twilio to be able to reach it, you will need a public URL. A good tool that we use to test webhooks is ngrok — https://www.youtube.com/watch?v=S1uExj7mMgM shows what it does and why we like it.
You now have everything you need to start using Twilio and Java!