How to Start Your First Java Spark App

September 15, 2021
Written by
Diane Phan
Twilion
Reviewed by

header - How to Start Your First Java Spark App

Setting up a new project to hack together an idea shouldn't be a problem, especially with the help of the Spark framework. Not only does this lightweight framework allow developers to be more productive with minimal boilerplate, but explore REST APIs at the same time.

In this article, you will learn how to navigate a Java IDE to set up and build a Java Spark application.

Tutorial requirements

Start a new Java project in IntelliJ

Open IntelliJ Idea and click on Create New Project.

IntelliJ IDEA home screen

Choose Gradle on the left hand side and check Java in the box on the right hand side.

choose gradle option for new java project

Give your project a name such as "FirstSparkApp" and click the Finish button.

Your project directory structure should look like the following image:

project directory for the build gradle file

Open the build.gradle file in the IDE and add the following lines inside the dependencies block:

    implementation group: "com.sparkjava", name: "spark-core", version: "2.9.3"
    implementation group: "org.slf4j", name: "slf4j-simple", version: "1.7.32"

Create a Hello World Java class

Expand the main subfolder under the src folder at the root of the project directory. Notice the empty java folder.

project directory for the java subfolder

Click on the File at the top of the IDE window. Hover over the New option and select Java Class as seen in the image below. Clicking this option will prompt you to give the class a name. Go ahead and name it "HelloWorld'.

screenshot to create a new java class in intellij

Open the file and copy and paste the following code to the file:

import static spark.Spark.*;

public class HelloWorld {
    public static void main(String[] args) {
        get("/", (req, res) -> "Hello World! ✨ It's fun to use sparkly emojis in code!");
    }
}

It is important to know that the filename must reflect the class name within the file. In the public HelloWorld class, a main method is invoked so that the code in the program starts once executed.

The Java Spark web application responds to all requests to the root URL "/" with a string message.


Feel free to change the string message, otherwise, go ahead and save the file.

Run the Java Spark application

Right click on the HelloWorld file in the project directory and find the option to Run 'HelloWorld.main()'. Wait a few seconds for the project to build and compile.

Spark automatically runs applications on port 4567 so head over to http://localhost:4567/ in your web browser to see the string message.

screenshot of the localhost:4567 spark app

Change the port for the Spark application

If you prefer to change the port number from "4567", you have to define the new port number before creating the route mappings.

Find the red square button to stop running the HelloWorld app. This can be found in the left panel below the directory.

stop button on INTELLIJ IDEA

Go back to the HelloWorld.java file and add the highlighted line to the main function:

    public static void main(String[] args) {
        port(8080);
        get("/", (req, res) -> "Hello World! ✨ It's fun to use sparkly emojis in code!");

The port is redefined to 8080 but feel free to change it to another port.

Save the file and run it again. You should be able to see the same string message on http://localhost:8080/ now.

What's next for building Java Spark projects?

Congratulations on building your first Java Spark application! But don't stop here. Try to send an SMS with Spark or check out these articles:  

Let me know about the projects you're building with Java and Twilio by reaching out to me over email!

Diane Phan is a software developer on the Developer Voices team. She loves to help programmers tackle difficult challenges that might prevent them from bringing their projects to life. She can be reached at dphan [at] twilio.com or LinkedIn.