user2738777
user2738777

Reputation: 510

Not able to run scala program even after compiling it

This is my scala code:

object world
{
    println("this is vaibhav")
}

i am able to compile it using scalac.

scalac object.scala

and these two files are generated:

world.class,world$.class

But when i run this:

scala world

it gives the follwing message:

java.lang.NoSuchMethodException: world.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1670)
    at   scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:74)
    at         scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
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)

Upvotes: 0

Views: 96

Answers (3)

Soumya Simanta
Soumya Simanta

Reputation: 11741

It is very unlikely that you will be using the Scala compiler (scalac) directly to build and manage any real Scala project. If you are starting with Scala I would suggest the following:

  1. Use a build tool such as sbt that will manage the dependecies and versions for you.
  2. Use an IDE (IntelliJ/Eclipse)

Upvotes: 0

bjfletcher
bjfletcher

Reputation: 11508

A couple of different ways with Scala. One using body of object:

object HelloWorld extends App {
  println("Hello World: " + (args mkString ", "))
}

Note the extends App there. See documentation about that.

The other using a main field of object:

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

Some documentation about that.

Upvotes: 2

cchantep
cchantep

Reputation: 9168

As a Java program needs a public static final main(String[] args) {} method to be executed, a Scala one needs a def main(args: Array[String]) {} in your object.

Upvotes: 1

Related Questions