Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Setting up a Local Dev Environment

To complete the Twilio Java quickstarts, you'll need to have the following tools installed:

  • Java
  • A web server capable of running Java Servlets
  • The twilio-java helper library

Install Java

If you are on a Mac or Linux machine, you probably already have Java installed. Otherwise, you can follow the excellent installation instructions on the Java website.

Install the twilio-java library

Twilio's Java helper library allows you to interact with the Twilio API while writing idiomatic Java code. There are two ways to install the twilio-java library.

Option 1: Use Maven

Maven is a tool that makes it easy to use third party Java libraries in your projects. Most computers will already have Maven installed. Mac and Linux users can check by running:

mvn -v
Apache Maven 3.0.4 (r1232337; 2012-01-17 00:44:56-0800)

in the Terminal. If you get back a version number, you're good to go. If you get back

mvn: Command not found

follow these Maven install instructions to get Maven on your Windows, Mac or Unix machine.

Once you have that, either set up a new pom.xml file for your project, or add the following <dependency> to your existing pom.xml file:

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
  <dependencies>
    <dependency>
      <groupId>com.twilio.sdk</groupId>
      <artifactId>twilio-java-sdk</artifactId>
      <version>(6.0,6.9)</version>
      <scope>compile</scope>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Then save this file and at the command line, run:

mvn install

Maven will automatically download the library.

Option 2: Download the .jar directly

If you're not running a Maven project, you can download the jar directly. Head to this twilio-java download page, and select the most recent version number. Then download the twilio-java-sdk-X.Y.Z-jar-with-dependencies.jar file.

This .jar file contains everything you need to write code using the twilio-java library. To use it, you'll have to add it to your Java classpath. One way is to add the following line to your .bashrc startup file.

.bashrc
export CLASSPATH=/path/to/twilio-java-sdk-3.3.10-SNAPSHOT-jar-with-dependencies.jar:$CLASSPATH

This way Java will automatically include the jar in its classpath for every command you run in the Terminal.

Alternatively, pass in the classpath as a command line option when compiling a Java file.

javac -cp /path/to/twilio-java-sdk-3.3.10-SNAPSHOT-jar-with-dependencies.jar:$CLASSPATH SendSms.java

If you use a tool such as Eclipse or IntelliJ, consult your documentation about how to add .jar files to your build path.

Installing a Java web server

To run the quickstart examples, you can use any server which can run Java Servlets. We will walk through installing Apache Tomcat, a popular web server that can run Java applications.

Download Tomcat

Head to the Apache Tomcat download page and download the Core Binary distribution. Then, navigate to the webapps folder inside the downloaded Tomcat directory. Create a new folder (servlet) inside the webapps directory called twilio, for our new servlet.

$ cd /path/to/apache-tomcat
# Navigate to the Apache Tomcat directory
$ mkdir -p webapps/twilio
# Create a new folder named twilio in the webapps directory.

We need to add three files to this new twilio directory. First, create a folder called WEB-INF. Inside that folder create two folders, one named lib and one named classes. Copy the twilio-java-sdk-3.3.10-SNAPSHOT-jar-with-dependencies.jar file to the lib directory.

$ cd /path/to/apache-tomcat/webapps/twilio
# Navigate to the Twilio servlet inside the webapps directory
$ mkdir -p WEB-INF/lib WEB-INF/classes
# Create two new folders that we're going to use later.
$ cp -v /path/to/twilio-java-sdk-3.3.10-SNAPSHOT-jar-with-dependencies.jar \
    WEB-INF/lib/
# Copy the twilio-java JAR file to the lib folder.

Then add the following HelloWorld.java file, creating folders as necessary:

webapps/twilio/WEB-INF/classes/com/twilio/HelloWorld.java
package com.twilio;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// This will print "Hello World" to the screen,
// so we can test that we have everything working
public class HelloWorld extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().print("Hello World");
    }
}

Compile that file to get a HelloWorld.class file. You may need to add the servlet-api.jar file in /path/to/apache-tomcat/lib/servlet-api.jar to your classpath before compiling.

$ cd /path/to/apache-tomcat/webapps/twilio
# Navigate to the twilio servlet folder
$ javac -cp /path/to/apache-tomcat/lib/servlet-api.jar:$CLASSPATH \
    WEB-INF/classes/com/twilio/HelloWorld.java
# Compile the HelloWorld.java file.

You also need a web.xml file to tell Tomcat where to route requests:

webapps/twilio/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>Hello, World Application</display-name>
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.twilio.HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

Test Tomcat

Now, start your Tomcat server.

$ cd /path/to/apache-tomcat
$ sh bin/startup.sh     # On Windows, run bin/startup.bat
# Starts the Tomcat server

Now open a browser and navigate to http://localhost:8080/twilio/hello. You should see "Hello World" in your browser.

Let's get started

Congrats, you now have everything you need to start using Twilio and Java! Let's start coding.

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 browsing the Twilio tag on Stack Overflow.