Reputation: 3782
I have the Scala program myTest.scala
with the following content:
class Test {
def hello() {
println("Hello, world!")
}
}
How can I execute it from the console? If I run scala myTest.scala
in the terminal, it obviously cannot find the main
method which I do not have.
Is there any way to run it as scala Test.hello
?
Is it mandatory to use scalac
to compile before running scala
?
Upvotes: 4
Views: 3426
Reputation: 3968
You need to first give your Object a main method. There are 2 ways to do this:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
or
object HelloWorld extends App {
println("Hello, world!")
}
Once you have your object ready. You can compile your class in your terminal as below:
$ scalac HelloWorld.scala
and run the program.
$ scala HelloWorld
Upvotes: 4
Reputation: 1868
You can't run a class in Scala.
You need to define an object with a main method or that extends App
object Test extends App {
println("Hello, world!")
}
Upvotes: 1