Product Catalog Application using Play and Scala Part 1 – Initial Setup

Tue, Jul 15, 2014

Play Programming #Play #scala

This is the first part of the tutorial series. We will be starting by installing and setting up Play framework.

First, you need to have JDK 6(at least) installed in your machine. Run the following command to check which version of JDK you have.

java -version

And you should get something like this

java version "1.7.0_60"
OpenJDK Runtime Environment (fedora-2.5.0.1.fc19-x86_64 u60-b30)
OpenJDK 64-Bit Server VM (build 24.60-b09, mixed mode)

Please note that your outcome might be different from mine. As long as the java version is > 1.6 then you will be fine. If you get a “command not found error”, chances are you don’t have JDK installed yet. There are plenty of tutorials on how to install JDK, so I will not cover it here. Please download JDK instead of JRE.

We will also need MySQL installed. Once you installed MySQL server, you can access it with root user and no password. We will set the user access later.

After this we will be looking on how to setup Play framework. There are 2 ways to setup Play framework in your machine, other than download the source and build it yourself. First one is to download the whole Play framework package, unzip it, and you will find a play.sh file in the folder(or play.bat if you are Windows user).

Another way is to use Activator(https://typesafe.com/activator), which is the preferred way, well at least for me. Activator is a tools to help you start application development using the template provided. So for the rest of the tutorial I will be using Activator.

First you will need to download Activator from here (http://www.playframework.com/download). Follow the instructions on the page on how to setup. It’s pretty straightforward actually. Once you are done, run the following command to verify your Activator installation.

activator --version

This command will print out the SBT launcher version.

sbt launcher version 0.13.5

By seeing this message means you’ve successfully installed Activator. Now we will be creating a new Play apps. Type the following command in your console.

activator new product-catalog

And you will be prompted with few options. Choose the “play-scala” option.

I will be using Activator console instead of Activator UI, in case you are wondering. It’s just my preference. You can use Activator UI if you want to, the code should be working fine on both mode.

If you are first time running this command, Activator will go and download all the libraries and dependencies from the source repository. It might take a while. After this when you create another project it will be much faster.

You will see the following messages after the project is setup

OK, application "product-catalog" is being created using the "play-scala" template.

To run "product-catalog" from the command line, "cd product-catalog" then:
/home/thor/Documents/projects/product-catalog/activator run

To run the test for "product-catalog" from the command line, "cd product-catalog" then:
/home/thor/Documents/projects/product-catalog/activator test

To run the Activator UI for "product-catalog" from the command line, "cd product-catalog" then:
/home/thor/Documents/projects/product-catalog/activator ui

And now we will go into the newly created directory and type “activator”.

[thor@localhost projects]$ cd product-catalog/
[thor@localhost product-catalog]$ activator
[info] Loading project definition from /home/thor/Documents/projects/product-catalog/project
[info] Updating {file:/home/thor/Documents/projects/product-catalog/project/}product-catalog-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to product-catalog (in build file:/home/thor/Documents/projects/product-catalog/)
[product-catalog] $ 

Let’s take a spin by starting the server. The default port is 8080 if we don’t specify any but I will be running on port 8000 instead as port 8080 has been occupied.

[product-catalog] $ run 8000
--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8000

(Server started, use Ctrl+D to stop and go back to the console...)

And we open up a browser and browse to http://localhost:8000/ and Voilà

Play Framework - A default welcome screen

Play Framework – A default welcome screen

And … we are done. The next part of the tutorial will cover some common commands we will be using when developing Play Framework apps.