scala SBT 错误:java.lang.RuntimeException:未检测到主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20778874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:58:08  来源:igfitidea点击:

SBT error: java.lang.RuntimeException: No main class detected

scalabuildsbt

提问by derek

Update:
I need put libraryDependencies for using Actor in Scala.

更新:
我需要放置 libraryDependencies 才能在 Scala 中使用 Actor。

libraryDependencies <++= scalaVersion(v =>
  Seq("org.scala-lang" % "scala-actors" % v)
)


when I ran "sbt run", I could successfully pass the compilation but then failed on run the code.

当我运行“sbt run”时,我可以成功通过编译,但在运行代码时失败。

Here is the error: java.lang.RuntimeException: No main class detected.

这是错误:java.lang.RuntimeException:未检测到主类。

The weird thing is when I do not use SBT, I can run it without any issue:

奇怪的是,当我不使用 SBT 时,我可以毫无问题地运行它:

>scalac actor.scala
>scala pingpong

Any idea why this happens?

知道为什么会这样吗?

I am using SBT 0.13.0 Scala version is 2.10.2

我使用的是 SBT 0.13.0 Scala 版本是 2.10.2

Thanks

谢谢

Deryk.

德里克。

Here is my code:

这是我的代码:

import scala.actors.Actor
import scala.actors.Actor._

case object Ping;
case object Pong;
case object Stop;

class Ping(count:Int, pong:Actor) extends Actor
{
    def act()
    {
        var counter = count -1 ;
        pong ! Ping;
        loop
        {
            react
            {
                case Pong =>
                if( (counter < count) && (counter > 0) ) {Console.println(counter+"->Ping: pong"); pong ! Ping; counter = counter -1;} 
                else {pong ! Stop;exit()}

            }
        }
    }
}

class Pong extends Actor
{
    def act()
    {
        loop
        {
            react
            {
               case Ping => {Console.println("Pong: Ping"); sender ! Pong;}
               case Stop => {Console.println("Ping Pong Communication is done!");exit()}
            }
        }
    }
}

object pingpong
{
    def main(args: Array[String])
    {
        println(util.Properties.versionString)
        val pong = new Pong

        val ping = new Ping(5, pong)

        ping.start
        pong.start
    }
}

采纳答案by derek

If you want to use Actor in Scala, you need include the library, as the following:

如果要在 Scala 中使用 Actor,则需要包含该库,如下所示:

libraryDependencies <++= scalaVersion(v =>
  Seq("org.scala-lang" % "scala-actors" % v)
)

回答by Vidya

Try this in your build file:

在你的构建文件中试试这个:

mainClass in (Compile,run) := Some("pingpong")

回答by Przemek

I have this exception if I try to use class instead of object.

如果我尝试使用类而不是对象,则会出现此异常。

//class Foo extends App {
//    print("No main class detected")
//}

object Foo extends App {
    print("Hello World")
}

回答by Mike Addison

It's looking for an object. Try creating a simple clock.scala object file and re-running it:

它正在寻找一个对象。尝试创建一个简单的 clock.scala 对象文件并重新运行它:

object Clock {
  def main(args: Array[String]) = println(new java.util.Date)
}

回答by Denys Lobur

You have to move your class which extends App to src/main/scalafolder. Otherwise you have to create mainmethod

您必须将扩展 App 的类移动到src/main/scala文件夹。否则你必须创建main方法