41

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?

Shaido
  • 27,497
  • 23
  • 70
  • 73
user2327621
  • 957
  • 3
  • 11
  • 15

3 Answers3

70

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.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
Apoorv Verma
  • 792
  • 6
  • 10
21

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
}
AmigoNico
  • 6,652
  • 1
  • 35
  • 45
  • Unfortunately there are other problems with using `App`: http://stackoverflow.com/questions/29133161/scala-member-field-visibility-in-spark-jobs . But for that, I agree that App is theoretically cleaner. – Ken Williams Apr 22 '15 at 15:55
3

When I want to test my code in intelligent idea scala editor, I simply create a companion object just below my class and put a main method in it. That's all. see an example:

class Colon {
    class Cow {
        def ^ (moon:Moon): Unit ={
        println("Cow jumped over the moon")
        }
    }
    class Moon{
        def ^:(cow:Cow) = println("This cow jumped over moon too")
    }
}
object Colon{
    def main(args: Array[String]): Unit = {
    val c:Colon = new Colon
    val cow = new c.Cow
    val moon = new c.Moon
    cow ^ moon
    cow ^: moon
    moon.^:(cow)
    }
}
abshar
  • 467
  • 1
  • 4
  • 8