Tutorial #1: Getting started with Play Framework 2.3.4

Reading Time: 3 minutes

In this blog we would set up the development environment with Play and Eclipse which would drive us through rest of the tutorial series.  We would be running this blog as a series and we would be looking at various aspects of play with this blog.

In this tutorial we will discuss about the following topics of Play Framework:

  • Installing Play
  • New application
  • Layout of Play application
  • Play console
  • Setting-up Eclipse

∗ Installing Play

Prerequisites: To run the Play framework, you need JDK 6 or later

Step-1: Download Activator from here

Step-2: Go to the ZIP file path and UNZIP it

anand@anand-singh:~$ cd softwares/
anand@anand-singh:~/softwares$ unzip typesafe-activator-1.2.10-minimal.zip
Archive:  typesafe-activator-1.2.10-minimal.zip
   creating: activator-1.2.10-minimal/
  inflating: activator-1.2.10-minimal/activator.bat  
  inflating: activator-1.2.10-minimal/activator  
  inflating: activator-1.2.10-minimal/activator-launch-1.2.10.jar  
anand@anand-singh:~/softwares$

Step-3: Set the activator to your path

anand@anand-singh:~$ vi .bashrc
export TYPESAFE_ACTIVATOR=/home/anand/softwares/activator-1.2.10-minimal
export PATH=$PATH:$TYPESAFE_ACTIVATOR

To check
anand@anand-singh:~$ which activator
/home/anand/softwares/activator-1.2.10-minimal/activator

Step-4: Check the activator command

anand@anand-singh:~$ activator -help

playingscala-tutorial-1

Now your system is ready to create a new Play application.

NOTE: If you don’t want to setup the activator into your path then just unzip and go to the activator script path and use it.

anand@anand-singh:~$ cd softwares/activator-1.2.10-minimal/
anand@anand-singh:~/softwares/activator-1.2.10-minimal$ ./activator -help

The result of "./activator -help" command is same like above screen.

∗ New application

There are several way to create new application using activator command:

$ activator new play-scala-tutorial-one play-scala

new_app

$ activator new play-scala-tutorial-one

Play Framework

∗ Layout of Play application

app                       Application sources
  assets                 Compiled asset sources
     stylesheets         Typically LESS CSS sources
     javascripts         Typically CoffeeScript sources
  controllers            Application controllers
  models                 Application business layer
  views                  Templates
build.sbt                 Application build script
conf                      Configurations files
  application.conf       Main configuration file
  routes                 Routes definition
public                    Public assets
  stylesheets            CSS files
  javascripts            Javascript files
  images                 Image files
project                   sbt configuration files
  build.properties       Marker for sbt project
  plugins.sbt            sbt plugins including play declaration
lib                       Unmanaged libraries dependencies
logs                      Standard logs folder
  application.log        Default log file
target                    Generated stuff
  scala-2.11.1            
     cache              
     classes             Compiled class files
     classes_managed     Managed class files (templates, ...)
     resource_managed    Managed resources (less, ...)
     src_managed         Generated sources (templates, ...)
test                      source folder for unit or functional tests

∗ Play Console

$ cd play-scala-tutorial-one
$ activator
[info] Loading project definition from /home/anand/projects/knoldus/play-scala-tutorial-one/project
[info] Set current project to play-scala-tutorial-one (in build file:/home/anand/projects/knoldus/play-scala-tutorial-one/)
[play-scala-tutorial-one] $ 

To Help:
[play-scala-tutorial-one] $ help run
Runs a main class, passing along arguments provided on the command line.
[play-scala-tutorial-one] $ help compile
Compiles sources.
[play-scala-tutorial-one] $ 


To Compile:
[play-scala-tutorial-one] $ compile
[success] Total time: 0 s, completed 3 Oct, 2014 12:11:34 PM
[play-scala-tutorial-one] $

The Play console is just a normal sbt console, so you can use sbt features such as triggered execution.

For example, using ~ compile

[play-scala-tutorial-one] $ ~ compile

The compilation will be triggered each time you change a source file.

If you are using ~ run

[play-scala-tutorial-one] $ ~ run

The triggered compilation will be enabled while a development server is running.

You can also do the same for ~ test, to continuously test your project each time you modify a source file:

[play-scala-tutorial-one] $ ~ test

 

∗ Setting-up Eclipse

[play-scala-tutorial-one] $ eclipse

Import the application into your Workspace with the File/Import/General/Existing project… menu (compile your project first).

Play Framework

This is the start of tutorial series, from next week onwards we would be working on this tutorial blog to make it grow. We would look at how we can make it more usable and readable, then we would be adding next tutorial of “Using WebJars, jQuery, Bootstrap & Bootswatch with Play application”. If you have any question then feel free to comment on the same 🙂 Stay tuned.

Next : Tutorial #2

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

15 thoughts on “Tutorial #1: Getting started with Play Framework 2.3.43 min read

  1. Hi,

    Thanks for your post. I am just getting started with Play 2.3.7. I am using Eclipse Juno with Scala IDE 3.0.4 on Mac OS 10.9

    I realised that the IDE does not recognise the syntax in the scala templates in the app/views folder. I have searched every where for the play plugin for eclipse, the one i found (https://github.com/scala-ide/scala-ide-play2) seems to be released for Play 2.1. I installed it but it failed to work.

    Also the IDE does not recognise the view templates on the controller (e.g index.render(“Hello World”) …index cannot be resolved).

    Please let me know where I can get the Play plugin or which free IDE I can use.

    Thanks

  2. There is a new version of Scala IDE 4.0.0 available now http://scala-ide.org/download/sdk.html . You should try that. Make sure to include the classes_managed folder in your libraries. This has all the templates that you would need for compilation. In my case the folder is under
    ./play-scala-tutorials/target/scala-2.11/classes_managed

    Go to project → properties → java build path → libraries (it’s a tab) → add external class folder

    and include that folder and you should be good. For more details refer to the discussion here http://stackoverflow.com/questions/10910215/play2-framework-my-template-is-not-seen-package-views-html-doesn-not-exist

  3. hi
    Thanks a bunch for the tutorial!!!
    Question: When working with eclipse, do we always want to leave activator running in ‘notice changed files mode’ (activator ~run) or can Eclipse handle this internally? The best development practice for Play 2 via Eclipse will always include ‘activator’ running in the background.
    FWIW, I have have followed the advice to have nightly IDE builds installed for Eclipse. help -> add new software -> add -> http://download.scala-ide.org/nightly-scala-ide-luna-211x
    For Kepler use http://download.scala-ide.org/sdk/lithium/e38/scala211/dev/site/

  4. https://docs.google.com/document/d/1gjH5TaQDKxbsrD4hzsMyUlHMq5LGFA0pletpdUwId7I/edit#heading=h.ucivqn1l6uit>Google Docs

    Eclipse preferences for Play 2 from Google Docs.
    =====================================
    Build automatically: disable (important!)
    Play performs byte code changes during compilation. If your class files are generated by Eclipse those will be missing and Play gets utterly confused.
    Refresh using native hooks: enable
    I believe this is required to constantly import the class files generated by Play.

    1. Hi,
      I’m trying to get sample for connecting rabbitmq amqp using play 2.2 java for windows but I couldn’t get it. is there any link for the same?.
      Kindly help me on this regards.
      Thank you.
      Prem

  5. Hi,
    I’m trying to start java play 2.5 project with eclipse in Linus Mint, but i am not able to start. i got following error

    [play-java-demo] $ compile
    [success] Total time: 1 s, completed Apr 19, 2016 11:36:41 AM
    [play-java-demo] $ eclipse
    [error] Not a valid command: eclipse (similar: help, alias)
    [error] Not a valid project ID: eclipse
    [error] Expected ‘:’ (if selecting a configuration)
    [error] Not a valid key: eclipse (similar: deliver, licenses, clean)
    [error] eclipse
    [error] ^
    [play-java-demo] $

    Kindly help me on this regards.

  6. Hi ,
    I’m trying to start java play 2.5 project in Linux Mint ,but i got an error .i used following steps

    ayur@kailash ~/ArvanProjects/TestPlayWork/play-java-demo $ activator
    [info] Loading project definition from /home/mayur/ArvanProjects/TestPlayWork/play-java-demo/project
    [info] Set current project to play-java-demo (in build file:/home/mayur/ArvanProjects/TestPlayWork/play-java-demo/)
    [play-java-demo] $ compile
    [success] Total time: 1 s, completed Apr 19, 2016 11:42:35 AM
    [play-java-demo] $ eclipse
    [error] Not a valid command: eclipse (similar: help, alias)
    [error] Not a valid project ID: eclipse
    [error] Expected ‘:’ (if selecting a configuration)
    [error] Not a valid key: eclipse (similar: deliver, licenses, clean)
    [error] eclipse
    [error] ^

  7. for the eclipse ( or more appropriately eclipsify the project) you will need to add the dependency as below.
    addSbtPlugin(“com.typesafe.sbteclipse” % “sbteclipse-plugin” % “4.0.0”)
    this will enable the eclipse command to create the eclipse project files.

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading