Play Framework 2

OS used : Windows 7
Play framework version used : 2.3.7

Play Console
It is a development console based on the build tool sbt (http://www.scala-sbt.org/)

1. To launch the play console, go to the project directory and type in 'activator' in command line
2. To run the current application type 'run' in the play console.



In this 'run' mode play will be running with auto reload feature ie for every requests Play will recompile the required resources and will restart the server if needed.
3. In Play the compilation would be automatically taken care by the server, but if you want to compile the files without running the server, even that is possible by running the command 'compile' in play console.


Compilation error
If you are using the play framework you can see the compilation errors on the browser directly and also on play console when we type in the run command.
It will display the line number, the file that has the compilation error and the details of the error.




4. If there are no compilation error on typing http://localhost:9000
it will load the following page




5. Debugging 

You can debug from the Play console by using the following command
$ activator -jvm-debug 9999
This will tell play to open a debug port. 
If you want to debug the application in Eclipse, then you need to create a Debug configuration for a Remote Java application with the port# 9999


6. The other 2 commands that would help is 'clean' and 'reload'.

How to open the projects in eclipse

1. Go to the folder where the project is downloaded.
2. Start the play console by typing in "activator"
3. Then use the "eclipse" command to eclipsify the project.

Changes to Build.sbt

If there are changes to build.sbt file, please make sure to reload the project by going to the play console for the project and typing in the command "reload"

How to build a Java Project in Play

1. Go to the project folder and 



For more info refer:
https://www.playframework.com/documentation/2.3.x/PlayConsole


How to debug Play 2.3.7 in Eclipse

Make sure that you have started the Play console and asked Play to open the debug port at 9999 by the command 'activator -jvm-debug 9999'

After that you need to set up a debug configuration in Eclipse which is as follows

1. Open the workspace that runs the play project in the eclipse
2. Click on Run > Debug Configuartions
3. In the Debug Configuration Right click on 'Remote Java Application' and select 'New'
4. Provide a name for the debug configuration and browse the Project in eclipse that you want to debug
5. Select the connection type as 'Standard', Host as ;localhost' and Port as '9999' . Remember that we have asked Play to open up a debug port on 9999
6. Click on Apply 
7. Now Click on Debug so that eclipse starts listening to that port.




Once you have set up the debug configuration and started debug using the configuration go back to Play console and run the server by the command 'run' and go to the browser and start the application by 'http://localhost:9000'. The Play would then load the required files and the control will come to the debug points you have in the application.


The following are the issues that I have come across while dealing with Play Framework 2

org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:9000




This is because some other service might be running at the same port, as you can see in the exception log Caused by: java.net.BindException: Address already in use: bind

2. Running Fake application in Play for Unit Test

a. Create a Junit Test class within the test folder.

b. Import the following at the top of the class
import org.junit.Test;

import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.running;

import static org.fest.assertions.Assertions.*;

c. Within the java class include the following code snippet

    @Test
public void testOffBottom() {
       running(fakeApplication(), new Runnable() {
@Override
public void run() {
    // Test should go here
assertThat
    }
 });

}

d. You can execute this test by going to play console and typing in "test"

For further read refer
https://www.playframework.com/documentation/2.0/JavaTest

When ever you are adding new references through build.sbt

1. Go to the play console corresponding to the project
2. Do a "reload", then "clean" and "update"
3. After that do "eclipse"
4. Go to Eclipse work space and refresh the project.


1 comment:

Richard Davis, Cleveland - OH said...

Excellent article . I have gone through different posts in the blog, keep up the good work.

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days