Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import play in scala repl

How can I import play in Scala repl?

scala> import play.api.libs.json._
<console>:11: error: not found: value play
       import play.api.libs.json._
like image 711
Manu Chadha Avatar asked Mar 11 '17 07:03

Manu Chadha


3 Answers

1) setup simple build tool(sbt) {its easy - download from here - http://www.scala-sbt.org/download.html and instructions here - http://www.scala-sbt.org/0.13/docs/Installing-sbt-on-Windows.html}

2) Create a empty folder with build.sbt with following contents

//your-test-project/build.sbt

scalaVersion := "2.11.8"                                                                               

resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"                    

libraryDependencies += "com.typesafe.play" %% "play" % "2.5.12"

3) Then simply do sbt console on the root on folder, which will download play and make it available to your console.

$ ls -l ~/.ivy2/cache/com.typesafe.play/play_2.11/jars/
total 15392
-rw-r--r--  1 as18  185223974  4107407 Jan 22 15:59 play_2.11-2.5.12.jar

Then you are good to go.

scala> import play.api.libs.json._
import play.api.libs.json._

scala> val json: JsValue = Json.parse("""{ "compiler" : "scala", "ratings" : 5 }""")
json: play.api.libs.json.JsValue = {"compiler":"scala","ratings":5}

scala> val compiler = ( json \ "compiler" )
compiler: play.api.libs.json.JsLookupResult = JsDefined("scala")

Also, you can directly provide the jar if you already have it as below

scala -cp ~/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.5.12.jar

scala> import play.api.libs._
import play.api.libs._
like image 79
prayagupa Avatar answered Nov 05 '22 11:11

prayagupa


Things are much simpler with Ammonite REPL:

load.ivy("com.typesafe.play" %% "play" % "2.5.12")
import whatever.you.need
like image 4
Iurii Ant Avatar answered Nov 05 '22 12:11

Iurii Ant


The package is not found because it is not in the class path of the REPL. If you know the location of Play Framework's JAR on your computer, you can add it to the class path when starting the REPL:

> scala -cp path/to/play.jar

You can also add this directly from inside a REPL session:

:require play.jar

Note that you will still need to import your classes as before.

like image 3
francoisr Avatar answered Nov 05 '22 10:11

francoisr