Menu

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

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.

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

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

Install a build tool

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 the this:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/twilio/.sdkman/candidates/maven/current
Java version: 15.0.1, vendor: AdoptOpenJDK, runtime: /home/twilio/.sdkman/candidates/java/15.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"

Install 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. There is also good Java support in Microsoft Visual Studio Code.

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

Create a Java project

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:

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, 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.

Create a Twilio application

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

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

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

<dependency>
  <groupId>com.twilio.sdk</groupId>
  <artifactId>twilio</artifactId>
  <version>8.0.0</version>
</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

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

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:

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

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, you can use some good online resources:

Serving content online

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 ngrokthis video shows what it does and why we like it.

Where next?

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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

Step 1

Get link

Get a free personal referral link here

Step 2

Give $10

Your user signs up and upgrade using link

Step 3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more