Skip to contentSkip to navigationSkip to topbar
Rate this page:
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:


_10
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:


_10
openjdk version "15.0.1" 2020-10-20
_10
OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.1+9)
_10
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:


_10
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:


_10
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
_10
Maven home: /home/twilio/.sdkman/candidates/maven/current
_10
Java version: 15.0.1, vendor: AdoptOpenJDK, runtime: /home/twilio/.sdkman/candidates/java/15.0.1.hs-adpt
_10
Default locale: en_GB, platform encoding: UTF-8
_10
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:


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

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:


_10
<dependency>
_10
<groupId>com.twilio.sdk</groupId>
_10
<artifactId>twilio</artifactId>
_10
<version>8.0.0</version>
_10
</dependency>

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


_10
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:


_10
spring init \
_10
--dependencies web \
_10
--groupId com.example \
_10
--artifactId my-first-twilio-project \
_10
--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:


_21
package com.example.myfirsttwilioproject;
_21
_21
import com.twilio.twiml.MessagingResponse;
_21
import com.twilio.twiml.messaging.Message;
_21
import org.springframework.stereotype.Controller;
_21
import org.springframework.web.bind.annotation.PostMapping;
_21
import org.springframework.web.bind.annotation.ResponseBody;
_21
_21
@Controller
_21
public class WebhookController {
_21
_21
@PostMapping("/hook")
_21
@ResponseBody
_21
public String getTwiML() {
_21
_21
return new MessagingResponse.Builder().message(
_21
new Message.Builder("Thanks for your message").build())
_21
.build()
_21
.toXml();
_21
}
_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!


Rate this page: