fozziethebeat
fozziethebeat

Reputation: 1180

Scala searches for an obscure Main class

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.

Upvotes: 2

Views: 359

Answers (2)

jesterhazy
jesterhazy

Reputation: 238

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.

Upvotes: 0

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297265

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.

Upvotes: 5

Related Questions