如何在托管环境中为Scala解释器设置类路径? [英] How to set up classpath for the Scala interpreter in a managed environment?

查看:96
本文介绍了如何在托管环境中为Scala解释器设置类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究扩展名适用于Apache Wicket Web框架,该框架允许用户在运行时从浏览器以几种编程语言执行代码。 Scala是其中一种语言,但是当将其打包为WAR文件并部署到诸如Tomcat之类的容器时,我会遇到麻烦。

I am working on an extension for the Apache Wicket web framework, which lets the user execute code in several programming languages at runtime from the browser. One of these languages is Scala, but I am having trouble when it is bundled as a WAR file and deployed to a container such as Tomcat.

调用Scala解释器时,它拒绝使用以下消息运行代码:

When the Scala interpreter is invoked, it refuses to run the code with the following message:

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.

在Scala设置上设置usejavacp后,它仍然无法在托管环境中运行。问题似乎是Scala解释器无法在Java类路径上找到Scala库jar。

After setting up usejavacp on the Scala settings, it still didn't work in a managed environment. The problem seems to be that the Scala interpreter cannot find the Scala library jars on the Java class path.

在网上搜索时,我发现了提案,提议使用两个名为 boot.class.path和 app.class.path的类路径资源,其中应包括所需的类路径声明。我尝试了一下,它似乎起作用了。但是,我对该解决方案的问题是,我的扩展名旨在捆绑到WAR文件中并在不同的环境中运行,因此用户有必要根据其所运行的环境来修改这些资源。将每个jar的路径都包含到文件中也将需要很多工作。

Searching the web, I found a proposal, which proposes the use of two classpath resouces named 'boot.class.path' and 'app.class.path', which should include the needed classpath declarations. I tried this and it seemed to work. My problem with this solution, though, is that my extension is meant to get bundled into a WAR file and to be run in different environments, so it would be necessary for the user to modifiy these resources with respect to the environment it runs in. Also it would be a lot of work to include every single jar's path into the file.

也许我不太理解这个建议。有人知道解决方案吗?

Maybe I don't understand fully the proposal. Does anybody know of a solution for this?

推荐答案

我已经设法将scala 2.9.1嵌入到tomcat中进行的战争中。

I have managed to embed scala 2.9.1 within a war running in tomcat.

这就是我的方法。

val code = """println("Hi");""";

val settings = new Settings
val compilerPath = java.lang.Class.forName("scala.tools.nsc.Interpreter").getProtectionDomain.getCodeSource.getLocation
val libPath = java.lang.Class.forName("scala.Some").getProtectionDomain.getCodeSource.getLocation

println("compilerPath=" + compilerPath);
println("settings.bootclasspath.value=" + settings.bootclasspath.value);

settings.bootclasspath.value = List(settings.bootclasspath.value, compilerPath, libPath) mkString java.io.File.pathSeparator    
settings.usejavacp.value = true
val interpreter = new IMain(settings)

interpreter.interpret(code);

仅用于搜索引擎。这些是我工作之前的例外。

Just for the search engines. These were my exceptions before it worked.

    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.

    Exception in thread "Thread-26" java.lang.Error: typeConstructor inapplicable for <none>
            at scala.tools.nsc.symtab.SymbolTable.abort(SymbolTable.scala:34)
            at scala.tools.nsc.symtab.Symbols$Symbol.typeConstructor(Symbols.scala:877)
            at scala.tools.nsc.symtab.Definitions$definitions$.scala$tools$nsc$symtab$Definitions$definitions$$booltype(Definitions.scala:157)
            at scala.tools.nsc.symtab.Definitions$definitions$.init(Definitions.scala:814)
            at scala.tools.nsc.Global$Run.<init>(Global.scala:697)
            at scala.tools.nsc.interpreter.IMain.scala$tools$nsc$interpreter$IMain$$_initialize(IMain.scala:114)
            at scala.tools.nsc.interpreter.IMain$$anonfun$initialize$1.apply$mcZ$sp(IMain.scala:127)
            at scala.tools.nsc.interpreter.IMain$$anonfun$initialize$2.apply(IMain.scala:126)
            at scala.tools.nsc.interpreter.IMain$$anonfun$initialize$2.apply(IMain.scala:126)
            at scala.concurrent.ThreadRunner$$anon$2$$anonfun$run$2.apply(ThreadRunner.scala:45)
            at scala.concurrent.ThreadRunner.scala$concurrent$ThreadRunner$$tryCatch(ThreadRunner.scala:31)
            at scala.concurrent.ThreadRunner$$anon$2.run(ThreadRunner.scala:45)
            at java.lang.Thread.run(Thread.java:662)

这篇关于如何在托管环境中为Scala解释器设置类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆