Ready to start sending and receiving phone calls with Twilio Programmable Voice using Java?
This Programmable Voice Quickstart for Java will teach you how to do this using the Twilio Java helper library for our REST API.
In this Quickstart, you will learn how to:
Prefer to get started by watching a video? Check out our Java Voice Quickstart video on Youtube.
If you already have a Twilio account and a voice-enabled Twilio phone number, you're all set here! Feel free to jump to the next step.
Before you can make a phone call from Java, you'll need to sign up for a Twilio account or sign into your existing account and purchase a voice-capable phone number.
If you don't currently own a Twilio phone number with voice functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "Voice" box and click "Search."
_10javac -version
You should see something similar to this output:
_10javac 1.8.0_92
The Twilio SDK requires Java SE 8 or higher, which will appear as version number "1.8
" or above when you run the above command.
If you have an older version of Java or no JDK at all, you'll need to install the JDK before going any further. Follow the directions for installing the Java SE Development Kit for your platform (Windows, Mac, Linux) from the Java SE Download Page.
Got Java all set up and ready to go? Fantastic!
Next, download the standalone Twilio Java Helper Library. With that library, you'll have Java classes that help you call out to Twilio API's using Java, along with all the other dependencies you'll need to get going. When you download the jar file, download the one with a name similar to twilio-8.x-jar-with-dependencies.jar.
Prefer to use Maven, Gradle, or another build tool?
With Maven, place the following within your <dependencies> tag in your pom.xml file:
_10<dependency>_10 <groupId>com.twilio.sdk</groupId>_10 <artifactId>twilio</artifactId>_10 <version>8.0.0</version>_10</dependency>
With Gradle, paste the next line inside the dependencies block of your build.gradle file:
_10compile group: "com.twilio.sdk", name: "twilio", version: "8.0.+"
If you are using a Java IDE to build your application, you can specify the group id, artifact id, and version number for your dependencies.
Regardless of the package manager you are using, remember to specify the latest version of the Twilio Java Helper Library - find the latest version number on the Twilio Java Helper Library page.
This Twilio Java helper library is not meant for use in Android applications! Use this library for servers, command line applications, and similar projects. Shipping your Twilio Account SID and Auth Token to end users is a security risk for your Twilio account.
If you want to make or receive phone calls from a mobile application (Android or iOS), you should use the Twilio Programmable Voice SDK. Check out that page for directions on how to get started!
Now that we have the JDK setup and the Twilio Java Helper library downloaded, we can make a phone call from the Twilio phone number we just purchased with a single API request. Create and open a new file called MakePhoneCall.java
and type or paste in this code sample.
You'll need to edit this file a little more before your phone call will work:
Swap the placeholder values for ACCOUNT_SID
and AUTH_TOKEN
with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you'll find your unique Account SID and Auth Token, which you'll need any time you make phone calls with the Twilio Java Helper Library. You can reveal your auth token by clicking on the eyeball icon:
Open MakePhoneCall.java
and replace the values for ACCOUNT_SID
and AUTH_TOKEN
with your unique values.
Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.
Remember that voice-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing from
number with that one, making sure to use E.164 formatting:
[+][country code][phone number including area code]
Replace the to
phone number with your phone number. This can be any phone number, but it's a good idea to test with your own phone so you can see the magic happen! As above, you should use E.164 formatting for this value.
Save your changes and compile this Java class from your terminal:
_10javac -cp twilio-8.0.0-jar-with-dependencies.jar MakePhoneCall.java
We need to include that Twilio jar with the dependencies to compile our class from the command line. If you are using an Integrated Development Environment (IDE) such as IntelliJ IDEA, Netbeans or Eclipse, you can simply add that Twilio jar to your classpath or your project libraries like any other dependency. You can also use a build tool like Maven or Gradle to build and run your Java application - just specify Twilio's helper library as a dependency.
Once you have the Java class built, you will need to run it - if you are running it from the command line on macOS or Linux, the command will look like this:
_10java -cp .:twilio-8.0.0-jar-with-dependencies.jar MakePhoneCall
On Windows, the equivalent command looks like this:
_10java -cp .;twilio-8.0.0-jar-with-dependencies.jar MakePhoneCall
The difference is that on Windows, the Java classpath separator on the command line is a semicolon, and on macOS or Linux, it is a colon.
That's it! In a few moments, you should receive a phone call from your Twilio number on your phone.
If you are on a Twilio Trial account, your outgoing voice calls are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs.
When your Twilio number receives an incoming phone call, Twilio will send an HTTP request to a server you control. This callback mechanism is known as a webhook. When Twilio sends your application a request, it expects a response in the TwiML XML format telling it how to respond to the message.
Let's see how we would build this in Java using the Spark web application framework. Spark is a lightweight web application framework and is completely different from the big data software named Apache Spark.
Spark requires its own library, separate from the Twilio Java helper library, which you can install using the directions on the Download Spark Java page. You'll find directions for using Spark with Maven, Gradle, or as a standalone download. Spark does not come with a logging implementation, but it does work with the Simple Logging Facade 4 Java (SLF4J), which you can also include in your Java classpath.
You can certainly set up all of your dependencies on the command line like we did in the above section when we made a phone call through Twilio, but it's a little easier to get everything set up using a Java integrated development environment (IDE). We are going to use the free, community edition of IntelliJ IDEA, which you can download from JetBrains. You can download JAR files and use them directly with IntelliJ IDEA, or you can use a dependency manager such as Maven or Gradle. We will use Gradle with IntelliJ IDEA to build our application.
If you haven't downloaded and installed IntelliJ IDEA Community Edition, please go ahead and do that now! After you run IDEA for the first time, you will see a screen that looks like this: