运行 Scala 控制台时出错。未找到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29296523/
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 07:00:48  来源:igfitidea点击:

Error running scala console. Module not found

scalaintellij-ideaintellij-14

提问by user3116355

I am able to run sample code, which I have saved in sample.scand the results are displaying on Scala console. But I get an error when running the following program, which I saved as the Timeprogram.scalascript:

我能够运行已保存的示例代码,sample.sc结果显示在 Scala 控制台上。但是在运行以下程序时出现错误,我将其保存为Timeprogram.scala脚本:

/**
 * Created by sarathrnair on 3/18/15.
 */


println ( "Enter the seconds" )

val totalSeconds=readInt()
val displaySeconds=totalSeconds%60
val totalMinutes=totalSeconds/60
val displayMinutes=totalMinutes%60
val displayHours=totalMinutes/60
val sec=displaySeconds.toString
val min=displayMinutes.toString
val finalString=displayHours+":"+("0"*(2-min.length))+min+":"+("0"*(2-sec.length))+sec

println (finalString)

The error I get is:

我得到的错误是:

Error running scala console. Module is not specified.

运行 Scala 控制台时出错。未指定模块。

Please help me determine the reason behind this error.

请帮助我确定此错误背后的原因。

采纳答案by Ajay Padala

If its a .scala file you need to wrap it into an object that extends App like:

如果它是一个 .scala 文件,你需要将它包装成一个扩展 App 的对象,如:

object Timeprogram extends App {
  // Your program here
}

回答by Stephen

The reason for the error mentioned when selecting "Run Scala Console", i.e. -

选择“运行Scala控制台”时提到的错误原因,即-

Error running scala console. Module is not specified.

运行 Scala 控制台时出错。未指定模块。

is that there is no module specified in the Scala Console Run Configuration. Click on the run configuration dropdown and select "Edit Configurations..." There you can specify a module.

是在 Scala 控制台运行配置中没有指定模块。单击运行配置下拉菜单并选择“编辑配置...”在那里您可以指定一个模块。

enter image description here

在此处输入图片说明

But this is just if you want to open a REPL shell within IntelliJ. If you just want to run the Scala program do it as in the accepted answer and just select "Run" instead of "Run Scala Console".

但这只是当您想在 IntelliJ 中打开 REPL shell 时。如果您只想运行 Scala 程序,请按照已接受的答案进行操作,只需选择“运行”而不是“运行 Scala 控制台”。

回答by Cass Q

Because you haven't set up the module yet. Open the configurations of the project and set this project as the module.

因为你还没有设置模块。打开工程的配置,将该工程设置为模块。