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

后端 未结 1 1962
小蘑菇
小蘑菇 2021-02-05 21:48

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 = {
    pri         


        
1条回答
  •  被撕碎了的回忆
    2021-02-05 22:42

    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);
        }
    }
    

    0 讨论(0)
提交回复
热议问题