Scala 搜索一个不起眼的 Main 类

发布于 2024-12-06 09:11:47 字数 4266 浏览 0 评论 0原文

我有一个简单的 scala 脚本,如下所示:

object SaveTaggedSenseTask {                                                    
  def main(args:  Array[String]) {
    val reader:SenseEvalAllWordsDocumentReader = new SenseEvalAllWordsDocumentReader()
    reader.parse(args(0))                                                   
    println(reader.sentences)                                               
    reader.sentences()
  }
}

SenseEvalAllWordsDocumentReader 是一个 java 定义的类,它只是 SAX 解析器的包装器。调用句子应该简单地返回另一个名为 Sentence 的 java 定义类的 java 列表。如果我使用运行此代码,

scala -cp jar-with-everything.jar SaveTaggedSenseTask.scala path/to/file.xml

我会得到以下可怕的混乱输出:

java.lang.ClassNotFoundException: Main (args = /home/stevens35/senseEval/senseEval3-allwords/english-all-words.xml, classpath = /tmp/scalascript7300484879512233483.tmp:/usr/java/jdk1.6.0_26/jre/lib/resources.jar:/usr/java/jdk1.6.0_26/jre/lib/rt.jar:/usr/java/jdk1.6.0_26/jre/lib/jsse.jar:/usr/java/jdk1.6.0_26/jre/lib/jce.jar:/usr/java/jdk1.6.0_26/jre/lib/charsets.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/jline.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-compiler.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-dbc.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-library.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scalap.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-swing.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/localedata.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/dnsns.jar:/home/stevens35/devel/C-Cat/wordnet/.:/home/stevens35/devel/C-Cat/wordnet/target/extendOntology-wordnet-1.0-jar-with-dependencies.jar:/home/stevens35/devel/C-Cat/wordnet/../data/target/extendOntology-data-1.0.jar)                                               
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:103)                                                                 
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
        at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)                                                       
        at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)                                                                      
        at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)                                                                      
        at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)                                                      
        at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)                                                             
        at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)                                                             
        at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
        at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)                                                                              
        at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
        at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)                                                                               
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)                                                                            
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

我假设这意味着 scala 正在寻找某个名为 Main 的类并且找不到它,但我不明白为什么它会认为寻找这个。此外,如果我删除 reader.sentences() 行,或者执行诸如调用 size() 之类的操作,问题就会消失。我只能猜测 scala 以某种方式推断出一个名为 Main 的类应该存在,因为这个调用,但我没有看到任何明显的解决方法。

想法?非常感谢任何帮助。

I have what should be a simple scala script that looks as follows:

object SaveTaggedSenseTask {                                                    
  def main(args:  Array[String]) {
    val reader:SenseEvalAllWordsDocumentReader = new SenseEvalAllWordsDocumentReader()
    reader.parse(args(0))                                                   
    println(reader.sentences)                                               
    reader.sentences()
  }
}

The SenseEvalAllWordsDocumentReader is a java defined class that is just a wrapper for a SAX parser. Calling sentences should simply return a java List of of another java defined class called Sentence. If i run this code using

scala -cp jar-with-everything.jar SaveTaggedSenseTask.scala path/to/file.xml

I get the following horrible mess of an output:

java.lang.ClassNotFoundException: Main (args = /home/stevens35/senseEval/senseEval3-allwords/english-all-words.xml, classpath = /tmp/scalascript7300484879512233483.tmp:/usr/java/jdk1.6.0_26/jre/lib/resources.jar:/usr/java/jdk1.6.0_26/jre/lib/rt.jar:/usr/java/jdk1.6.0_26/jre/lib/jsse.jar:/usr/java/jdk1.6.0_26/jre/lib/jce.jar:/usr/java/jdk1.6.0_26/jre/lib/charsets.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/jline.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-compiler.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-dbc.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-library.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scalap.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-swing.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/localedata.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/dnsns.jar:/home/stevens35/devel/C-Cat/wordnet/.:/home/stevens35/devel/C-Cat/wordnet/target/extendOntology-wordnet-1.0-jar-with-dependencies.jar:/home/stevens35/devel/C-Cat/wordnet/../data/target/extendOntology-data-1.0.jar)                                               
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:103)                                                                 
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
        at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$runCompiled(ScriptRunner.scala:171)                                                       
        at scala.tools.nsc.ScriptRunner$anonfun$runScript$1.apply(ScriptRunner.scala:188)                                                                      
        at scala.tools.nsc.ScriptRunner$anonfun$runScript$1.apply(ScriptRunner.scala:188)                                                                      
        at scala.tools.nsc.ScriptRunner$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)                                                      
        at scala.tools.nsc.ScriptRunner$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)                                                             
        at scala.tools.nsc.ScriptRunner$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)                                                             
        at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
        at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)                                                                              
        at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
        at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)                                                                               
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)                                                                            
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

I'm assuming that this means scala is looking for some class named Main and can't find it, but I can't figure out why it would even think of looking for this. Furthermore, if i delete the reader.sentences() line, or do something like call size() on it, the problem goes away. I can only guess that scala is somehow inferring that a class named Main should exist due to this call, but I don't see any obvious workaround.

Thoughts? Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-12-13 09:11:47

您正在混淆 Scala 脚本和 scala 程序。当使用objectdef main时,应该使用scalac编译程序,然后使用scala传递调用它包含 main 方法的对象的名称。

当作为脚本调用时,您应该删除 objectdef main,然后只放置您的程序。参数仍将位于 args 中。

现在,Scala 2.9.1 的功能之一是可以混合脚本和程序调用,但由于某种原因它显然在这里不起作用。我建议你选择一种做事方式,并坚持下去。

You are conflating Scala script and scala program. When using object and def main, you should compile the program with scalac, and then call it with scala passing the name of the object that contains the main method.

When calling as a script, you should remove object and def main, and just put your program. The arguments will still be in args.

Now, one of the features of Scala 2.9.1 is that one could mix script and program invocation, but it is obviously not working here for some reason. I suggest you pick one way of doing things, and stick to that.

妄断弥空 2024-12-13 09:11:47

您的脚本定义了对象 SaveTaggedSenseTask,但不执行任何操作。

只需添加

SaveTaggedSenseTask.main(args)

到脚本文件的末尾即可。或者您可以删除 object 和 def 语句,就像 @daniel-c-sobral 建议的那样。

Your script defines the object SaveTaggedSenseTask, but doesn't execute anything.

Just add

SaveTaggedSenseTask.main(args)

to the end of the script file. Or you can remove the object and def statements, like @daniel-c-sobral suggested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文