I wrote a Scala class and defined the main()
method in it. It compiled, but when I ran it, I got NoSuchMethodError:main
. In all the scala examples, I have seen, the main method is defined in an object. In Java we define the main method in a class. Is it possible to define main()
in a Scala class or do we always need an object for this?
The main() method is Scala's code execution entry point. It is similar to the main of C, C++, and Java. As the name implies, this method is the main entry point for executing any Scala code.
The Scala compiler generates a program from an @main method f as follows: It creates a class named f in the package where the @main method was found. The class has a static method main with the usual signature of a Java main method: it takes an Array[String] as argument and returns Unit .
The @main Annotation. Scala 3 has introduced the annotation @main to allow a method to be converted into the entry point of an executable program. If we save helloWorld as a file, like HelloWorld. scala, we can run it simply by executing scala helloWorld, after compiling it with scalac.
To answer your question, have a look on the following : I made a scala class, compiled and decompiled it, and what I got is interesting.
class MyScalaClass{ def main(args: Array[String]): Unit = { println("Hello from main of class") } } Compiled from "MyScalaClass.scala" public class MyScalaClass { public void main(java.lang.String[]); public MyScalaClass(); }
So it means that when the scala class is converted to java class then the main method of the scala class which in turn being converted to the main method in java class is not static. And hence we would not be able to run the program because JVM is not able to find the starting point in the program.
But if the same code is done by using the 'object' keyword then:
Compiling the following: object MyScalaClass{ def main(args: Array[String]): Unit = { println("Hello from main of object") } } Decompiling the following: javap MyScalaClass$.class Compiled from "MyScalaClass.scala" public final class MyScalaClass$ { public static final MyScalaClass$ MODULE$; public static {}; public void main(java.lang.String[]); } Decompiling the following javap MyScalaClass.class Compiled from "MyScalaClass.scala" public final class MyScalaClass { public static void main(java.lang.String[]); }
So here we got public static void main in MyScalaClass.class therefore the main method can be executed directly by the JVM here.
I hope you got your answer.
As Eugene said in a comment, there are no static methods in Scala. But watch this:
$ cat Echo.scala object Echo { def main( args:Array[String] ):Unit = args foreach println } $ scalac Echo.scala $ javap Echo\$.class Compiled from "Echo.scala" public final class Echo$ { public static final Echo$ MODULE$; public static {}; public void main(java.lang.String[]); } $ javap Echo.class Compiled from "Echo.scala" public final class Echo { public static void main(java.lang.String[]); }
Note that the classfile for the Echo class (not Echo$, the object) does indeed have a public static void main method. Scala generates static methods for methods defined in objects for compatibility with Java.
However, I consider creating a main method in a Scala program an anachronism. Use the App trait instead; it's cleaner:
object Echo extends App { args foreach println }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With