嵌入式 Scala REPL 继承父类路径 [英] Embedded Scala REPL inherits parent classpath

查看:51
本文介绍了嵌入式 Scala REPL 继承父类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this thread 在 Scala 邮件列表中,如何创建继承父程序类路径的嵌入式 Scala REPL?假设使用 scala -cp 启动父 Scala 程序....; 可以作为字符串访问并用于初始化嵌入的 REPL 吗?(通过 System.getProperty("java.class.path") 获得的 Java 类路径似乎与 Scala 类路径不同.)

As asked in this thread on the Scala mailing list, how can I create an embedded Scala REPL that inherits the classpath of the parent program? Suppose the parent Scala program is launched using scala -cp <classpath> ...; can <classpath> be accessed as a string and used to initialize the embedded REPL? (The Java classpath, available via System.getProperty("java.class.path"), appears to differ from the Scala classpath.)

或者,也许嵌入式 Scala REPL 可以从父进程继承或构造其 ClassLoader(Michael Dürig 的 ScalaDays 2010 演讲可能相关).这是推荐的方法吗?

Alternatively, perhaps the embedded Scala REPL can inherit or construct its ClassLoader from the parent process (Michael Dürig's ScalaDays 2010 talk might be relevant). Is this the recommended approach?

推荐答案

我正在尝试做同样的事情,我刚刚通过 谷歌搜索:

I'm trying to do the same thing, and I just found a way my out by Googling:

lazy val urls = java.lang.Thread.currentThread.getContextClassLoader match {
  case cl: java.net.URLClassLoader => cl.getURLs.toList
  case _ => error("classloader is not a URLClassLoader")
}
lazy val classpath = urls map {_.toString}

以上代码为您提供当前上下文中的类路径.

The above code gets you the classpath in current context.

settings.classpath.value = classpath.distinct.mkString(java.io.File.pathSeparator)

将其放入您的 settings.classpath 中,您应该能够启动 dispatch 或您需要的任何库.

Put that into your settings.classpath and you should be able to fire up dispatch or whatever library you need.

这篇关于嵌入式 Scala REPL 继承父类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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