我必须在Play Framework应用程序中使用scala解析器。
import scala.tools.nsc._
trait Foo
class Parser {
def parse(code: String) = {
val settings = new Settings
settings.embeddedDefaults[Foo]
val interpreter = new Interpreter(settings)
interpreter.parse(code)
}
}我在Build.scala中有下面的依赖项
"org.scala-lang" % "scala-compiler" % "2.9.1"此代码在使用SBT构建时工作。在游戏中,它以NullPointerException结尾,并且:
未能初始化编译器:对象scala未找到。 **请注意,从2.8开始,scala并不假定使用java类路径。 **对于旧行为,将-usejavacp传递给scala,或者如果使用设置 **对象编程,settings.usejavacp.value = true。
Build.scala
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "com.qwerty.utils"
val appVersion = "1.0-SNAPSHOT"
val scalaVersion = "2.9.1"
val appDependencies = Seq(
"org.scala-lang" % "scala-compiler" % "2.9.1"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
// Add your own project settings here
)
}发布于 2012-06-10 20:47:04
有关embeddedDefaults的背景信息,请参阅原提案。
容器(Play)必须定义'app.class.path‘和'boot.class.path’资源,然后embeddedDefaults将使用它们为环境正确地配置解释器。所以,这是一种增强的游戏。
如果可以将必要的类路径传递到应用程序中,则可以使用以下内容显式地配置类路径和类加载器:
val settings = new Settings
settings.classpath.value = "<classpath>"
settings.bootclasspath.value =
settings.bootclasspath.value + File.pathSeparator +
"<extra-bootclasspath>"
val interpreter = new Interpreter(settings) {
override def parentClassLoader = classOf[Foo].getClassLoader
}
interpreter.parse(code)引导路径通常应该包含scala ary.jar,类路径应该包含应用程序jars。
https://stackoverflow.com/questions/10642084
复制相似问题