簡體   English   中英

運行Scala程序報錯:Main method not found in class main,請定義main方法

[英]Error in running Scala Program: Main method not found in class main, please define the main method

我是 Scala 的新手。我所做的是,我在 Ubuntu 20.04 上安裝了Scala 2.13.6、SBT 1.5.5Intellij 2021.2 我在 Intellij 中安裝了插件 scala 和 sbt executor。 當我創建一個新的 Scala class 並將這段代碼寫入其中時,我可以毫無錯誤地運行它:

object main extends App{
    println("Hello")
}

但是當我有這段代碼時,我無法運行它:

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

錯誤如下:

Error: Main method is not static in class exm, please define the main method as:public static void main(String[] args)

我知道 IDE 認為代碼是 Java,但為什么呢?

任何幫助將非常感激。

主要的 function 需要在 object 中,而不是 class 定義中。 所以而不是這個

package main_function_test

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

寫這個:

package main_function_test

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM