I already posted this question in scala lang forum but unfortunately I did not get any answer. Second chance ?
I try to embed an interpreter an evaluate a scala snippet into this interpreter. I would like to bind an instance of a custom class within the interpreter. To sum up that would look like:
import scala.tools.nsc._
import scala.tools.nsc.interpreter._
class C {
def sayHello(s:String) = "hello "+s
}
object Main extends App {
val c= new C
val s = new Settings
s.usejavacp.value=true
val i = new IMain(s)
i.bind("myC",c)
i.bind("world","the world")
val script = "println(myC.sayHello(world))"
i.eval(script)
}
When I run this snippet inside Eclipse (Kepler) - OpenJDK6/7 works for both - BSD OS Scala-2.11.0-M4 - scala-compiler.jar in the path it works fine If I try to run the same code inside the repl or directly with a scalac file.scala then scala -cp . Main I get the following error
error: not found value myC
javax.script.ScriptException: compile-time error
at scala.tools.nsc.interpreter.IMain.compile(IMain.scala:575)
at scala.tools.nsc.interpreter.IMain.eval(IMain.scala:997)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
I was able to make it work under another OS (Win7) but by adding s.bootclasspath="path/to/my/classes"
I suspect some classpath issue
Later I was able to make it run by replacing the scala command line call by a java call this way:
java -cp $CLASSPATH Main with CLASSPATH containing scala libraries it works
I looked at scala command and it is like it appends scala libraries in the java path in a different way.
Does anyone has any advice ?
Thanks
Added based on following comments:
Scalac does not output any error In fact if I run:
java -cp .:$SCALA_PATH/lib/scala-library.jar:$SCALA_PATH/lib/scala-compiler.jar:$SCALA_PATH/lib/scala-reflect.jar Main
or as suggested scala -nobootcp it works (Thanks for the valuable advice) Otherwise if I let scala use the bootcp the started line is the following one and it fails
java -Xbootclasspath/a:/usr/home/pcohen/Dev/Scala/scala-2.11.0-M4/lib/akka-actors.jar[...] -classpath "" [...]
When the scala jars are appended to the bootclasspath, it is like my binding is failing. I am not able to clearly understand why this bootclasspath difference affects my classes.