2

This one should be simple, but I can't figure it out myself.

I have Scala 2.9.1 installed on an Ubuntu 12.04 system.

The file is helloworld.scala:

object HelloWorld{
        def main(args:Array[String]){
                println("Hello, World!")
        }
}

scalac helloworld works totally fine without issue. Then scala HelloWorld gives

Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: HelloWorld
    at scala.sys.package$.error(package.scala:27)
    at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
    at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
    at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
    at scala.Option.getOrElse(Option.scala:108)
    at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
    at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

I've seen this question several times on forums but the poster is usually just mixing the class name and the file name so they would be trying scala helloworld, this is not what I'm doing (right?) but I get the same error.

Edit

scala -classpath ./ HelloWorld works fine. So it's a classpath problem.

4

3 に答える 3

3

I tried this and got exactly the same. Then I cleared my classpath thus:

$ CLASSPATH=

and all worked.

于 2012-08-21T10:56:57.763 に答える
1

あなたが尋ねたものではありませんが、多くの依存関係があるので、sbtを使用してクラスパスについて心配させる方が良いでしょう。

于 2012-08-21T21:02:37.227 に答える
1

I'm also using Ubuntu and testing with your example I can confirm that:

scala HelloWorld.scala

will run the file successfully as a Scala script. And:

scalac HelloWorld.scala

will create the class files.

But, running:

scala HelloWorld.class

will give the error you are getting.

However, if you are running

scala HelloWorld

in the directory that has the class file in it, then the program should execute without a problem.

于 2012-08-21T11:59:18.090 に答える