I have a problem with scala.tools.sbt
scala file
Here I used parser functionality to make abstract syntax tree of code 2 + 3
import scala.tools.nsc._
object Main extends App {
var i = new Interpreter
println(i.parse("2 + 3"))
}
SBT configuration
name := "scalaSample"
version := "1.0-SNAPSHOT"
scalaVersion := "2.9.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.1" % "test"
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.9.1"
Error
Failed to initialize compiler: object scala 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 java.lang.NullPointerException at scala.tools.nsc.CompilationUnits$CompilationUnit. (CompilationUnits.scala:16) at scala.tools.nsc.interpreter.ExprTyper$codeParser$.applyRule(ExprTyper.scala:22) at scala.tools.nsc.interpreter.ExprTyper$codeParser$.stmts(ExprTyper.scala:36) at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:47) at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:46) at scala.tools.nsc.reporters.Reporter.withIncompleteHandler(Reporter.scala:46) at scala.tools.nsc.interpreter.ExprTyper$class.parse(ExprTyper.scala:46) at scala.tools.nsc.interpreter.IMain$exprTyper$.parse(IMain.scala:1012) at scala.tools.nsc.interpreter.IMain.parse(IMain.scala:1013) at eu.semantiq.scalaToJS.Main$delayedInit$body.apply(Main.scala:7) at scala.Function0$class.apply$mcV$sp(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:60) at scala.App$$anonfun$main$1.apply(App.scala:60) at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) at scala.collection.immutable.List.foreach(List.scala:45) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30) at scala.App$class.main(App.scala:60) at eu.semantiq.scalaToJS.Main$.main(Main.scala:5) at eu.semantiq.scalaToJS.Main.main(Main.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) java.lang.RuntimeException: Nonzero exit code: 1 at scala.sys.package$.error(package.scala:27)
In scala REPL everything works
Welcome to Scala version 2.9.0.1 (OpenJDK 64-Bit Server VM, Java 1.6.0_23). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.tools.nsc._
import scala.tools.nsc._
scala> var i = new Interpreter
warning: there were 4 deprecation warnings; re-run with -deprecation for details warning: there were 1 deprecation warnings; re-run with -deprecation for details
i: scala.tools.nsc.Interpreter = scala.tools.nsc.Interpreter@786bfd73
scala> println(i.parse("2 + 3"))
Some(List(2.$plus(3)))
I feel really sorry for my bad English
According to xsbt's FAQ:
sbt runs tests in the same JVM as sbt itself and Scala classes are not in the same class loader as the application classes.
And there's more:
The key is to initialize the Settings for the interpreter using embeddedDefaults.
The example that is given there uses some arbitrary type MyType. In fact, you can use any of your types to help sbt find the appropriate class loader (see this answer).
Hence, your code should look like this:
import scala.tools.nsc._
trait Foo // Arbitrary type added to get stuff working
object Main extends App {
val settings = new Settings
settings.embeddedDefaults[Foo]
val interpreter = new Interpreter(settings)
println(interpreter.parse("2 + 3"))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With