Play 2 Scala Console on Heroku

I’ve been working on a Play 2 application that I’ll be using for the Grails vs Play Smackdown at ÜberConf next week. The app is running in production on Heroku but since I don’t have an admin UI yet, I needed a quick and easy way to create a new entity. I could have gone straight to the database but thought it would be better to run Play 2’s Scala Console on Heroku and then just run some arbitrary Scala code. The Scala Console in Play 2 is really just the Scala REPL in SBT but it allows you to interact with a Play application.

I’ll walk you through how to use the Play 2 Scala Console on Heroku. If you’d like to follow along, grab the play2bars app by running:

git clone
cd play2bars
git checkout java-ebean

To run the console locally, run:

play -DapplyEvolutions.default=true console

You will see something like:

[info] Loading project definition from /home/jamesw/Desktop/play2bars/project
[info] Set current project to play2bars-java (in build file:/home/jamesw/Desktop/play2bars/)
[info] Updating {file:/home/jamesw/Desktop/play2bars/}play2bars-java...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin                                                                                [info] Done updating.                                                        
[info] Compiling 4 Scala sources and 3 Java sources to /home/jamesw/Desktop/play2bars/target/scala-2.9.1/classes...
[warn] Note: /home/jamesw/Desktop/play2bars/app/models/ uses unchecked or unsafe operations.
[warn] Note: Recompile with -Xlint:unchecked for details.
[info] Starting scala interpreter...
Welcome to Scala version (OpenJDK 64-Bit Server VM, Java 1.7.0_03).
Type in expressions to have them evaluated.
Type :help for more information.

Start a Play app from the Scala console:

new play.core.StaticApplication(new"."))

You will see something like:

[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Prod)
res0: play.core.StaticApplication = play.core.StaticApplication@2e338c56

Now you can interact with the Play application. Lets create a new “Bar” entity:

var bar = new models.Bar() = "foo bar"

And now you can query the “Bar” entities:

import scala.collection.JavaConversions._
models.Bar.find.all.foreach(bar => println(

So that’s pretty cool, right?

If you want to run this example on Heroku, then install the Heroku Toolbelt and run:

heroku create -s cedar

And push the application to Heroku:

git push heroku java-ebean:master

Once the application is built and deployed on Heroku, check to make sure it works:

heroku open

To run the Scala console on Heroku, run:

heroku run bash
java -DapplyEvolutions.default=true -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL -jar ~/.sbt_home/bin/sbt-launch-0.11.3-2.jar

SBT will resolve some dependencies and then you will be in the SBT console. From there run:

set fullClasspath in Compile += Attributed.blank(file("target/staged/*"))

Now you will be in the Scala REPL for your Play app on Heroku. So you can run things like:

new play.core.StaticApplication(new"."))
var bar = new models.Bar() = "foo bar"

Reload your app on Heroku in the browser and you should now see the new Bar listed on the page!

Let me know if you have any questions.

  • James Ong

    That nice James Ward, too bad that isn’t a poll system!

    Love Scala too but what do you think about Play 2.0 Scala status that is ready for enterprise when I’m not sure how secure it is?

    How on earth we both came from AS3 world to Scala? :)

    • I think Play 2 with Java or Scala is great!

      It’s nice to have a head start on the functional world of Scala – thanks to AS3!  :)

      • James Ong

        Yeah. Kudo to AS3!

        On the other thing, after experimenting Scala, I wondering if 
        Actionscript  4 (speculating) could consider revamp most of the syntax to Scala? I wish this is possible to have OO and FP in AS4 as well. Oh, I must be dreaming.

  • Kevin

    James, nice entry.  Showing of the flexibility of Play 2 with Scala is very encouraging.

    I successfully followed the steps (i.e. everything worked locally) until the ‘git push heroku java-ebean:master’step. At which point I see:

    —–> Heroku receiving push
    —–> Play 2.0 – Java app detected
    —–> Building app with sbt
    —–> Running: sbt clean compile stage

    [info] Done updating.
    [info] Compiling 1 Scala source to /tmp/build_26o4gatwfjtpr/.sbt_home/.sbt/plugins/target/scala-2.9.1/sbt-0.11.3/classes…
    [info] Loading project definition from /tmp/build_26o4gatwfjtpr/project

    At this point things grind to a halt, and eventually I receive a timeout / failed to compile message:

    /app/slug-compiler/lib/utils.rb:65:in `block (2 levels) in spawn’: command=’/app/slug-compiler/lib/../buildpacks/scala/bin/compile /tmp/build_2bqwmjpve52ri /app/tmp/repo.git/.cache’ exit_status=0 out=” at=timeout elapsed=537.3300747871399 (Utils::TimeoutError)
    from /app/slug-compiler/lib/utils.rb:51:in `loop’
    from /app/slug-compiler/lib/utils.rb:51:in `block in spawn’
    from /app/slug-compiler/lib/utils.rb:47:in `popen’
    from /app/slug-compiler/lib/utils.rb:47:in `spawn’
    from /app/slug-compiler/lib/buildpack.rb:38:in `block in compile’
    from /app/slug-compiler/lib/buildpack.rb:36:in `fork’
    from /app/slug-compiler/lib/buildpack.rb:36:in `compile’
    from /app/slug-compiler/lib/slug.rb:446:in `block in run_buildpack’
    from /app/slug-compiler/lib/utils.rb:120:in `log’
    from /app/slug-compiler/lib/slug.rb:684:in `log’
    from /app/slug-compiler/lib/slug.rb:445:in `run_buildpack’
    from /app/slug-compiler/lib/slug.rb:110:in `block (2 levels) in compile’
    from /app/slug-compiler/lib/utils.rb:101:in `block in timeout’
    from /usr/local/lib/ruby/1.9.1/timeout.rb:58:in `timeout’
    from /app/slug-compiler/lib/utils.rb:101:in `rescue in timeout’
    from /app/slug-compiler/lib/utils.rb:96:in `timeout’
    from /app/slug-compiler/lib/slug.rb:99:in `block in compile’
    from /app/slug-compiler/lib/utils.rb:120:in `log’
    from /app/slug-compiler/lib/slug.rb:684:in `log’
    from /app/slug-compiler/lib/slug.rb:98:in `compile’
    from /app/slug-compiler/bin/slugc:85:in `block in ‘
    from /app/slug-compiler/lib/slug.rb:454:in `block in lock’
    from /app/slug-compiler/lib/repo_lock.rb:44:in `call’
    from /app/slug-compiler/lib/repo_lock.rb:44:in `run’
    from /app/slug-compiler/lib/slug.rb:454:in `lock’
    from /app/slug-compiler/bin/slugc:66:in `’
    ! Heroku push rejected, failed to compile Play 2.0 – java app

    Any ideas how to proceed?

    • Can you try again?  This may have been a temporary issue.

      • Kevin

        I’ve tried again and it is now working!  Thanks.

         I’ve been prototyping with a Scala/Play/Heroku/Neo4j/Postgres server side stack and trying to assess maturity and stability.  Definitely has a bleeding edge feel, yet I’m still becoming convinced that things are being built on a pretty solid bedrock.  Documentation is very sparse, so I sure appreciate your blog efforts, they are probably the glue that has held things together this long.

        Looking forward to more entries!

  • In you Procfile, you should use -Dconfig.resource=akka-sync.conf instead of -Dconfig.resource=conf/akka-sync.conf.

  • I’m using play 2.1-SNAPSHOT

    After typing “console” I get:

    Failed to initialize compiler: class scala.reflect.BeanInfo not found.
    ** Note that as of 2.8 scala does not assume use of the java classpath.
    ** For the old behavior pass -usejavacp to scala, or if using a Settings
    ** object programatically, settings.usejavacp.value = true.
    [error] (run-main) java.lang.NullPointerException

    ***stack trace***

    Is it because I’m using the latest release instead of the stable one? The scala version is 2.10 instead of 2.9 which may create troubles I guess…

    • Are you having this problem when running locally? Or just on Heroku?

    • Same here. No play2 console for 2.1 :(

      • It’s worth trying 2.1.1 which just came out. But there may not be a fix in there. Perhaps this is just Scala version incompatibilities.