Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call main method of a Scala program from the main method of a java program?

Tags:

java

scala

Suppose I have a Scala class and a Java class in a Java project and the scala class is like below

class Sam {

  def main(args: Array[String]): Unit = {
    println("Hello")
  }

}

How can I call it's main method from the main method of a java program which is present in the same project

like image 843
yAsH Avatar asked May 21 '13 15:05

yAsH


1 Answers

Typically, main methods are static in Java, and in an object in Scala. This allows you to run them from the command line. Your code defines a class, not an object.

I'd suggest changing your Scala code to:

object Sam {
  def main(args: Array[String]): Unit = {
    println("Hello")
  }
}

You can then call this from your Java main method as follows:

class Foo  {
    public static void main(String[] args) {
        Sam.main(args);
    }
}
like image 200
Martin Ellis Avatar answered Oct 24 '22 19:10

Martin Ellis