9

I googled a lot and am totally stuck now. I know, that there are similar questions but please read to the end. I have tried all proposed solutions and none did work.

I am trying to use the IMain class from scala.tools.nsc within a Play 2.1 project (Using Scala 2.10.0).

Controller Code

This is the code, where I try to use the IMain in a Websocket. This is only for testing.

object Scala extends Controller {
  def session = WebSocket.using[String] { request =>
    val interpreter = new IMain() 
    val (out,channel) = Concurrent.broadcast[String]
    val in = Iteratee.foreach[String]{ code =>
      interpreter.interpret(code) match {
        case Results.Error =>      channel.push("error")
        case Results.Incomplete => channel.push("incomplete")
        case Results.Success =>    channel.push("success")
      }      
    } 
    (in,out)
  }
}

As soon as something gets sent over the Websocket the following error gets logged by play:

Failed to initialize compiler: object scala.runtime in compiler mirror 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.

Build.scala

object ApplicationBuild extends Build {
  val appName         = "escalator"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    "org.scala-lang" % "scala-compiler" % "2.10.0"
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(    
  )
}

What I have tried so far

All this didn't work:

I dont know what to do now.

Community
  • 1
  • 1
Martin Ring
  • 5,404
  • 24
  • 47
  • have you tried something simpler like (new scala.tools.nsc.interpreter.IMain()).interpret("val x = 1") match ... – Iraklis May 12 '13 at 20:40
  • @Iraklis yes. I put that in the initialization code in Global.scala. Same error message. – Martin Ring May 12 '13 at 20:47
  • You may also want to look at my answer [here](http://stackoverflow.com/a/23925201/3686016) regarding this issue. – Aniket May 29 '14 at 03:28

2 Answers2

3

The problem here is that sbt doesnt add scala-library to the class path. The following workaround works. First create a folder lib in the top project directory(the parent of app,conf etc) and copy there the scala-library.jar

Then you can use the following code to host an interpreter :

      val settings = new Settings
      settings.bootclasspath.value +=scala.tools.util.PathResolver.Environment.javaBootClassPath + File.pathSeparator + "lib/scala-library.jar"
      val in = new IMain(settings){
            override protected def parentClassLoader = settings.getClass.getClassLoader()
      }
     val res = in.interpret("val x = 1")

The above creates the bootclasspath by adding to the java class the scala library. It's not a problem with play framework it comes from the sbt. The same problem occures for any scala project when it runs with sbt. Tested with a simple project. When it runs from eclipse its works fine.

EDIT: Link to sample project demonstrating the above.`

Martin Ring
  • 5,404
  • 24
  • 47
Iraklis
  • 810
  • 5
  • 14
  • doesnt work for me. now the error message changed to `scala.annotation.Annotation` from `scala.runtime`... but still the same issue. – Martin Ring May 13 '13 at 08:00
  • 1
    you might havent copy the correct scala-library.jar. if you want i cant send you the porject i used. – Iraklis May 13 '13 at 08:05
  • i used the 2.10.0 scala-library. that would be awesome. you can find my mail address in my profile. – Martin Ring May 13 '13 at 08:06
  • thank you so much for your effort! i'll have a look into it now. :) – Martin Ring May 13 '13 at 08:24
  • ok. i just ran the app and got the exact same error when i opened `/echo´ . so it has to have something todo with the outer setup... I dont know what else to try. :( – Martin Ring May 13 '13 at 08:30
  • 1
    whats is your operating system? maybe try the following instead of ":" write File.pathSeperator – Iraklis May 13 '13 at 09:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29829/discussion-between-iraklis-and-martin-ring) – Iraklis May 13 '13 at 09:26
-1

I wonder if the reflect jar is missing. Try adding this too in appDependencies.

"org.scala-lang" % "scala-reflect" % "2.10.0"
huynhjl
  • 41,520
  • 14
  • 105
  • 158