Aman
Aman

Reputation: 3261

How to execute scala class from eclipse?

Hi I am new to scala and i am trying to execute a scala class from eclipse.
I have created a class and not object.

package classexample


class sample extends App {
  def printSample(): Unit = {
    println("Sample example")
  }

  def main(args: Array[String]) {
    val v = new sample;
    v.printSample()
  }

}

Now how do I execute this program.When I execute this this does not give any result.
Am i doing it in wrong way or i am missing something....Thanks for your help

Upvotes: 1

Views: 1353

Answers (1)

Joost den Boer
Joost den Boer

Reputation: 5047

When you have the Scala plugin installed in Eclipse, you can start an executable Scala in the same way you do for a Java executable. See scala-ide.org from info about this.

In Scala, you do not have to implement the 'main' method as for Java. Just extending the 'App' trait is sufficient. Any code in the constructor, which is all code in the class except val's, var's or def's, will be executed when the class is run.

Here an example of a runnable Scala class which prints 'Hello World'.

object RunnableScalaClass extends App {
  println("Hello World")
}

Upvotes: 2

Related Questions