【发布时间】:2014-05-02 18:10:22
【问题描述】:
使用 scopt https://github.com/scopt/scopt
我有一个非常简单的 Scala CLI 驱动程序,它在 .parse 的第一行出错。该行是 var i = 0,无法想象为什么会失败,也许是我如何实例化 OptionParser?
def parse(args: Seq[String], init: C): Option[C] = {
var i = 0 <———————————————— prints the error below
val pendingOptions = ListBuffer() ++ (nonArgs filterNot {_.hasParent})
Exception in thread "main" java.lang.NoSuchMethodError: scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;
at scopt.OptionParser.parse(options.scala:306)
at org.apache.mahout.drivers.ItemSimilarityDriver$.main(ItemSimilarityDriver.scala:47)
at org.apache.mahout.drivers.ItemSimilarityDriver.main(ItemSimilarityDriver.scala)
这里有完整的代码,抱歉,我是 Scala 新手,所以这可能是一个非常愚蠢的问题
object ItemSimilarityDriver {
/**
* @param args Command line args, if empty a help message is printed.
* @return
*/
def main(args: Array[String]): Unit = {
val parser = new OptionParser[Config]("ItemSimilarity") {
head("ItemSimilarity", "Spark")
opt[Unit]('r', "recursive") action { (_, c) =>
c.copy(recursive = true) } text("The input path should be searched recursively for files that match the filename pattern (optional), Default: false.")
opt[String]('o', "output") required() action { (x, c) =>
c.copy(output = x) } text("Output is a path for all output (required)")
opt[String]('i', "input") required() action { (x, c) =>
c.copy(input = x) } text("Input is a path for input, it may be a filename or dir name. If a directory it will be searched for files matching the -p pattern. (required)")
note("some notes.\n")
help("help") text("prints this usage text")
}
// parser.parse returns Option[C]
parser.parse(args, Config()) map { config => <—————————— parser was created
but this call fails in the parse
// do stuff
//val didIt = true
} getOrElse {
// arguments are bad, error message will have been displayed, throw exception, run away!
}
}
case class Config(recursive: Boolean = false, input: String = null, output: String = null)
}
我也尝试了可变选项方法,但出现了同样的错误。
【问题讨论】:
-
错误是
java.lang.NoSuchMethodError。您使用的是什么构建工具?您是否包含了与 scopt 匹配的 Scala 库版本? -
在 Intellij 中运行,它使用 pom 添加库,使用 Scala 2.10.3,这似乎在 scopt 的 build.sbt 中得到支持。我的项目中有 scala-library-2.10.3.jar。
-
如果我回到 scopt 2.10 似乎可以工作,即 maven 神器。我去看看能不能升级一下项目,不过太大了