Skip to contentSkip to navigationSkip to topbar
On this page

How to Set up a Local Java Development Environment


Setting up a development environment for Java can be daunting, so let's 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 Integrated Development Environment (IDE) will provide a lot of help: syntax highlighting, refactoring assistance, integration with your build tool, and more.

Once you have these installed, you can then create Twilio applications in Java.


Installation

installation page anchor

An excellent way to install the Java distribution and build tool is with SDKMAN!(link takes you to an external page). 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.

Install a Java distribution

install-a-java-distribution page anchor

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(link takes you to an external page), but you will need to set the JAVA_HOME(link takes you to an external page) 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:

1
openjdk version "15.0.1" 2020-10-20
2
OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.1+9)
3
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15.0.1+9, mixed mode, sharing)

The two most popular build tools are Maven(link takes you to an external page) and Gradle(link takes you to an external page). 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(link takes you to an external page) and install(link takes you to an external page) it yourself.

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

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

The most popular IDE for Java is Intellij IDEA(link takes you to an external page). The community edition is free and very powerful. There are other IDEs — if you are familiar with Eclipse(link takes you to an external page) or Netbeans(link takes you to an external page) then feel free to use them too. There is also good Java support in Microsoft Visual Studio Code(link takes you to an external page).

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:

1
public class HelloTwilio {
2
public static void main(String[] args) {
3
System.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(link takes you to an external page), or r/learnjava(link takes you to an external page) and r/javahelp(link takes you to an external page) on Reddit.


Create a Twilio application

create-a-twilio-application page anchor

Your applications can interact with Twilio in two ways:

  • Call the Twilio API from your code.
  • Have Twilio call your application using a webhook .

Many applications will do both.

Call the Twilio API from your code

call-the-twilio-api-from-your-code page anchor

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.

Add the Twilio helper library

add-the-twilio-helper-library page anchor

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'

How to use the Twilio helper library

how-to-use-the-twilio-helper-library page anchor

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.

Accept webhook calls from Twilio

accept-webhook-calls-from-twilio page anchor

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(link takes you to an external page), 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:

1
spring init \
2
--dependencies web \
3
--groupId com.example \
4
--artifactId my-first-twilio-project \
5
--extract

If you are using the online Spring Initializr(link takes you to an external page), 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:

1
package com.example.myfirsttwilioproject;
2
3
import com.twilio.twiml.MessagingResponse;
4
import com.twilio.twiml.messaging.Message;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.PostMapping;
7
import org.springframework.web.bind.annotation.ResponseBody;
8
9
@Controller
10
public class WebhookController {
11
12
@PostMapping("/hook")
13
@ResponseBody
14
public String getTwiML() {
15
16
return new MessagingResponse.Builder().message(
17
new 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(link takes you to an external page)https://www.youtube.com/watch?v=S1uExj7mMgM(link takes you to an external page) shows what it does and why we like it.


You now have everything you need to start using Twilio and Java!