scala 如何在 SBT 0.13 项目中设置主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15891708/
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
how to set main class in SBT 0.13 project
提问by expert
Could you guys please explain to me how to set main class in SBT project ? I'm trying to use version 0.13.
你们能否向我解释如何在 SBT 项目中设置主类?我正在尝试使用 0.13 版。
My directory structure is very simple (unlike SBT's documentation). In the root folder I have build.sbtwith following content
我的目录结构非常简单(与 SBT 的文档不同)。在根文件夹中,我有build.sbt以下内容
name := "sbt_test"
version := "1.0"
scalaVersion := "2.10.1-local"
autoScalaLibrary := false
scalaHome := Some(file("/Program Files (x86)/scala/"))
mainClass := Some("Hi")
libraryDependencies ++= Seq(
"org.scalatest" % "scalatest_2.10" % "2.0.M5b" % "test"
)
EclipseKeys.withSource := true
And I have subfolder projectwith single file Hi.scalawhich contains following code
我有包含以下代码的project单个文件的子文件夹Hi.scala
object Hi {
def main(args: Array[String]) = println("Hi!")
}
I'm able to compile it by calling sbt compilebut sbt runreturns
我可以通过调用编译它sbt compile但sbt run返回
The system cannot find the file C:\work\externals\sbt\bin\sbtconfig.txt.
[info] Loading project definition from C:\work\test_projects\sbt_test\project
[info] Set current project to sbt_test (in build file:/C:/work/test_projects/sbt_test/)
java.lang.RuntimeException: No main class detected.
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed Apr 8, 2013 6:14:41 PM
采纳答案by Jed Wesley-Smith
You need to put your application's source in src/main/scala/, project/is for build definition code.
您需要将应用程序的源代码放在 中src/main/scala/,project/用于构建定义代码。
回答by Artem Ignatiev
Try to use an object and extend it from App instead of using class
尝试使用一个对象并从 App 扩展它而不是使用类
object Main extends App {
println("Hello from main scala object")
}
because you need to run main method neither main class
因为您既不需要运行 main 方法,也不需要运行 main 类
回答by expert
Here is how to specify main class
这是指定主类的方法
mainClass in (Compile,run) := Some("my.fully.qualified.MainClassName")
mainClass in (Compile,run) := Some("my.fully.qualified.MainClassName")
回答by Marvin.Hansen
For custom modules in SBT (0.13), just enter on the SBT console:
对于 SBT (0.13) 中的自定义模块,只需在 SBT 控制台上输入:
project moduleX
[info] Set current project to moduleX (in build file:/path/to/Projects/)
> run
[info] Running main
to switch scope to moduleX, as define in Built.scala. All main classes within that scope will be detected automatically. Otherwise you get the same error of no main class detected. For God's sake, SBT does not tell you that the default scope is not set. It has nothing to do with default versus non default source folders but only with SBT not telling anything that it doesn't know which module to use by default.
将范围切换到 moduleX,如 Built.scala 中定义的那样。将自动检测该范围内的所有主要类。否则,您会收到未检测到主类的相同错误。看在上帝的份上,SBT 不会告诉您未设置默认范围。它与默认和非默认源文件夹无关,只是与 SBT 不告诉任何它不知道默认使用哪个模块有关。
Big Hint to typesafe: PLEASE add a default output like:
类型安全的重要提示:请添加一个默认输出,如:
[info] Project module is not set. Please use ''project moduleX'' set scope
or set in Built file (LinkToDocu)
at the end of SBT start to lower the level of frustration while using SBT on multi module projects.....
在 SBT 结束时开始降低在多模块项目中使用 SBT 时的挫败感.....
回答by Saeed Zarinfam
If you have multiple main methods in your project you can add the following line to your build.sbt file:
如果您的项目中有多个主要方法,您可以将以下行添加到您的 build.sbt 文件中:
val projectMainClass = "com.saeed.ApplicationMain"
mainClass in (Compile, run) := Some(projectMainClass)
If you want to specify the class that will be added to the manifest when your application is packaged as a JAR file, add this line to your build.sbt file:
如果要指定在将应用程序打包为 JAR 文件时将添加到清单中的类,请将此行添加到 build.sbt 文件中:
mainClass in (Compile, packageBin) := Some(projectMainClass)
You can also specify main class using run-main command in sbt and activator to run:
您还可以在 sbt 和 activator 中使用 run-main 命令指定主类来运行:
sbt "run-main com.saeed.ApplicationMain"
or
或者
activator "run-main com.saeed.ApplicationMain"
回答by conny
I had the same issue: was mode following the tutorial at http://www.scala-sbt.org/0.13/docs/Hello.html, and in my opinion, as a build tool sbt's interaction and error messages can be quite misleading to a newcomer.
我有同样的问题:是模式遵循http://www.scala-sbt.org/0.13/docs/Hello.html 上的教程,在我看来,作为构建工具sbt的交互和错误消息可能相当误导新人。
It turned out, hours of head scratching later, that I missed the critical cd helloline in the example each time. :-(
事实证明,经过几个小时的挠头,我cd hello每次都错过了示例中的关键行。:-(
回答by Sergii Shevchyk
There are 4 options
有4个选项
you have 1 main class
sbt runand sbt will find main for you
you have 2 or more main classes
sbt runand sbt will propose to select which one you want to run.
你有 1 个主要课程
sbt run和 sbt 会为你找到 main
您有 2 个或更多主要课程
sbt run并且 sbt 会建议选择您要运行的那个。
Multiple main classes detected, select one to run:
[1] a.b.DummyMain1
[2] a.b.DummyMain2
Enter number:
You want to set main class manually.
mainClass in run := Some("a.b.DummyMain1")You could run with main class as parameter
sbt runMain a.b.DummyMain1
您想手动设置主类。
mainClass in run := Some("a.b.DummyMain1")您可以使用主类作为参数运行
sbt runMain a.b.DummyMain1
回答by Langdon
If the Main class is in a different project then by setting the below command in build.sbtwould work:
如果 Main 类在不同的项目中,那么通过在其中设置以下命令build.sbt将起作用:
addCommandAlias("run", "; project_folder/run")
回答by kulsin
I had the same issue. Resolved it after adding PlayMinimalJavaplugin in build.sbt.
我遇到过同样的问题。添加PlayMinimalJava插件后解决build.sbt。
Not sure how it got fixed, if someone can highlight how PlayMinimalJavasolves it, would be great.
不确定它是如何修复的,如果有人能强调如何PlayMinimalJava解决它,那就太好了。
enablePlugins(PlayMinimalJava)
My build.sbtlooks like this
我的build.sbt看起来像这样
organization := "org"
version := "1.0-SNAPSHOT"
scalaVersion := "2.13.1"
libraryDependencies += guice
enablePlugins(PlayMinimalJava)
Log
日志
C:\Users\KulwantSingh\repository\pdfdemo>sbt run
Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading settings for project pdfdemo-build from plugins.sbt ...
[info] Loading project definition from C:\Users\KulwantSingh\repository\pdfdemo\project
[info] Loading settings for project pdfdemo from build.sbt ...
[info] Set current project to pdfdemo (in build file:/C:/Users/KulwantSingh/repository/pdfdemo/)
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
--- (Running the application, auto-reloading is enabled) ---
[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
[info] Compiling 6 Scala sources and 2 Java sources to C:\Users\KulwantSingh\repository\pdfdemo\target\scala-2.13\classes ...
[info] p.a.h.EnabledFilters - Enabled Filters (see <https://www.playframework.com/documentation/latest/Filters>):
play.filters.csrf.CSRFFilter
play.filters.headers.SecurityHeadersFilter
play.filters.hosts.AllowedHostsFilter
[info] play.api.Play - Application started (Dev) (no global state)

