David Portabella David Portabella - 9 months ago 77
Scala Question

how to have a Scala standalone application that uses the playframework libraries

I have this simple test Scala application, which a blocking http request:


name := "hello"

version := "1.0"

scalaVersion := "2.11.2"

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.4.0-M1"


import play.api.libs.json._
import play.api.libs.ws._
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

object Test {
def main(args: Array[String]) = {
val wsClient = WS.client
val body = getBody(wsClient.url("http://example.com/").get())
println(s"body: $body")

def getBody(future: Future[WSResponse]) = {
val response = Await.result(future, Duration.Inf);
if (response.status != 200)
throw new Exception(response.statusText);

this application fails with:
Exception in thread "main" java.lang.RuntimeException: There is no started application

how to solve this issue?


EDIT for Play 2.5:

This has changed a bit since Play 2.5 runs on AHC 2.0.x now. Please see:

for standalone WSClient usage. If you are migrating from earlier versions, see https://www.playframework.com/documentation/2.5.x/Migration25#Play-WS-upgrades-to-AsyncHttpClient-2

For Play 2.4:

Do not use raw AsyncHttpClientConfig.Builder for HTTPS -- it does not configure a secure SSLContext with hostname validation.

You can create a new WSClient instance using the following code:

import play.api.libs.ws.ning._
import play.api.libs.ws._

val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build()
val builder = new AsyncHttpClientConfig.Builder(config)
val wsClient:WSClient = new NingWSClient(builder.build())

Please note that this will start up threads which will not be closed until you close the client:


and you may run into memory leaks if you don't close it.