Scala 2.9 won't run HelloWorld on Ubuntu 12.04

1k Views Asked by At

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.

3

There are 3 best solutions below

3
On BEST ANSWER

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

$ CLASSPATH=

and all worked.

1
On

Not what you asked, but since you have many dependencies, it would be better to use sbt and let it worry about classpaths.

1
On

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.