I am totally new in Scala. What I have done is that, I have installed Scala 2.13.6 , SBT 1.5.5 and Intellij 2021.2 on Ubuntu 20.04. I have installed plugin scala and sbt executor in Intellij. When I create a new Scala class and write this code to it, I can run that without any errors:
object main extends App{
println("Hello")
}
But when I have this code, I cannot run it:
class exm {
def main(args: Array[String]): Unit={
println("Hello world")
}
}
The error is in the following:
Error: Main method is not static in class exm, please define the main method as:public static void main(String[] args)
I know that IDE think the code is Java, but why?
Any help would be really appreciated.
The main function needs to be inside an object, not a class definition. So instead of this
package main_function_test
class exm {
def main(args: Array[String]): Unit = {
println("Hello world")
}
}
write this:
package main_function_test
object exm {
def main(args: Array[String]): Unit = {
println("Hello world")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.