Scala SBT: scala.tools.nsc not working

I have a problem with scala.tools.sbt

scala file

Here I used the parser function to create an abstract syntax tree of 2 + 3 code

 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: scala object not found. ** Please note that with 2.8 scala does not imply the use of the javapathpath class. ** For old behavior, skip -usejavacp to scala, or if you use the Settings ** object programmatically, settings.usejavacp.value = true.

[error] (run-main) java.lang.NullPointerException java.lang.NullPointerException with 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 ) in scala.tools.nsc.interpreter.ExprTyper $$ anonfun $ parsing $ 2.Apply (ExprTyper.scala: 47) in scala.tools.nsc.interpreter.ExprTyper $$ anonfun $ parsing $ 2.Apply (ExprTyper.scala : 46) in scala.tools.nsc.reporters.Reporter.withIncompleteHandler (Reporter.scala: 46) in scala.tools.nsc.interpreter.ExprTyper $ class.parse (ExprTyper.scala: 46) in scala.tools.nsc. interpreter.IMain $ exprTyper $ .parse (IMain.scala: 1012) in scala.tools.nsc.interpreter.IMain.parse (IMain.scala: 1013) in 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 $ ΞV $ sp (AbstractFunction0.scala: 12) in scala.App $$ ano nfun $ main $ 1.apply (App.scala: 60) in scala.App $$ anonfun $ main $ 1.apply (App.scala: 60) in scala.collection.LinearSeqOptimized $ class.foreach (LinearSeqOptimized.scala: 59) in scala.collection.immutable.List.foreach (List.scala: 45) in scala.collection.generic.TraversableForwarder $ class.foreach (TraversableForwarder.scala: 30) in 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) in sun.reflect. NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) in sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:616 null javaang Exit: 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). Enter expressions to evaluate them. Type: help for more information.

scala> import scala.tools.nsc._

import scala.tools.nsc._

scala> var i = new interpreter

warning: there were 4 cancellations of the warning; re-run with -deprecation for more information: 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'm sorry for my bad english

+6
source share
1 answer

According to the xsbt FAQ :

sbt runs the tests in the same JVM as sbt itself, and the Scala classes are not in the same classloader as the application classes.

And further:

The key is to initialize the settings for the interpreter using embeddedDefaults.

The above example uses the arbitrary type MyType . In fact, you can use any of your types to help sbt find the appropriate class loader (see this answer ).

Therefore, 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")) } 
+4
source

Source: https://habr.com/ru/post/908712/


All Articles