我已经创建了一个网络应用,当我尝试使用 sbt run
,它显示。
sbt run
[warn] No sbt.version set in project/build.properties, base directory: /home/developer/scala/user-svc/target/scala-2.13
[info] Loading global plugins from /home/developer/.sbt/1.0/plugins
[info] Set current project to scala-2-13 (in build file:/home/developer/scala/user-svc/target/scala-2.13/)
[error] java.lang.RuntimeException: No main class detected.
[error] at scala.sys.package$.error(package.scala:30)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] (Compile / bgRun) No main class detected.
[error] Total time: 0 s, completed May 23, 2020, 11:44:32 PM
内容是: build.sbt
是。
val Http4sVersion = "0.21.4"
val CirceVersion = "0.13.0"
val Specs2Version = "4.9.3"
val LogbackVersion = "1.2.3"
lazy val root = (project in file("."))
.settings(
organization := "io.example",
name := "user-svc",
version := "0.0.1-SNAPSHOT",
scalaVersion := "2.13.2",
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-jetty" % Http4sVersion,
"org.http4s" %% "http4s-jetty-client" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"org.http4s" %% "http4s-dsl" % Http4sVersion,
"io.circe" %% "circe-generic" % CirceVersion,
"org.specs2" %% "specs2-core" % Specs2Version % "test",
"ch.qos.logback" % "logback-classic" % LogbackVersion
),
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3"),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"),
)
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-language:higherKinds",
"-language:postfixOps",
"-feature",
"-Xfatal-warnings",
)
文件夹结构应该是正确的
我到底做错了什么?
你没有定义一个主类,也就是程序的入口点。在JVM上 你要有 至少有一个类,它定义了
public static void main(args: String[])
在Scala的情况下,它的意思是类似于
object Main {
def main(args: Array[String]): Unit = {
...
}
}
如果正好有一个这样的类,sbt可以检测到它,如果有更多的类,你必须告诉sbt使用哪一个。
mainClass := Some("name.of.my.Main")
告诉sbt使用哪个类,以便调用 run
. 如果你运行JAR,这个类可以使用 manifest.mf
或明确通过。