无法在 Intellij IDE 中的 Scala 中找到或加载主类

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

Could not find or load main class in scala in intellij IDE

scalaintellij-ideasbt

提问by sariii

I have searched for this error, but the answers were for Java, but my case is Scala. I am trying to run this project in IntelliJ IDE sentimenAnalysis, but it throws an error. This is also the structure of the project. Class Not found

我已经搜索过这个错误,但答案是针对 Java 的,但我的案例是 Scala。我试图在 IntelliJ IDE sentimenAnalysis 中运行这个项目 ,但它抛出了一个错误。这也是项目的结构。未找到类

Update 1According to answers, Adding $ at the end of the name of search class enter image description here

更新 1根据答案,在搜索类名称的末尾添加 $ 在此处输入图片说明

Update 2after adding sbt task: enter image description here

添加 sbt 任务后更新 2在此处输入图片说明

Update 3My problem was resolved by importing the project, instead of getting it directly from GitHub, probably the manual configuration that Mike Allen said could resolve the problem, but I couldn't successfully apply that.

更新3我的问题是通过导入项目解决的,而不是直接从GitHub获取,可能是Mike Allen说的手动配置可以解决问题,但我无法成功应用。

采纳答案by SeriousDron

Probably you imported project somehow wrong. I see scala folder is not highlighted as sources. Usually, you don't even need to setup Run configuration manually for Scala SBT project. Do you have Scala and SBT Plugins in your IDE?

可能您导入的项目不知何故错误。我看到 scala 文件夹没有突出显示为来源。通常,您甚至不需要为 Scala SBT 项目手动设置运行配置。您的 IDE 中是否有 Scala 和 SBT 插件?

You should open SBT projects through "Import project" and choose "build.sbt" file. I would also recommend enabling auto-import to install all of the dependicies.

您应该通过“导入项目”打开 SBT 项目并选择“build.sbt”文件。我还建议启用自动导入以安装所有依赖项。

That's how it look for me: Here how it looks for me

这就是它寻找我的方式: 这里是我的样子

If you still wanna make it manually your configuration should work. That's my Run configuration that works:

如果您仍然想手动进行配置,您的配置应该可以工作。这是我的运行配置:

My run configuration

我的运行配置

回答by Laura Corssac

My problem was resolved when I marked src folder as sourced root. Click with the right button in src folder -> Mark Directory as -> Sources root

当我将 src 文件夹标记为源根目录时,我的问题得到了解决。在 src 文件夹中单击右键 -> 将目录标记为 -> 源根目录

回答by alltej

You will get this error if you tried to open the project and imported it incorrectly. I would open the project like this in Intellij:

如果您尝试打开项目并错误地导入它,您将收到此错误。我会在 Intellij 中打开这样的项目:

File>New> Project from Existing Source>(select) Import project from external model>
(select)sbt

click Next>Finish

回答by Tomer Ben David

In my case the objectin the object MyObject extends Appwas nested, if you have it unested meaning in your scala file it's not under any other object it made it work.

在我的情况下,objectinobject MyObject extends App是嵌套的,如果您在 Scala 文件中有未嵌套的含义,则它不在任何其他对象下,它使它起作用。

Netsted caused this error in intellij:

Netsted 在 intellij 中引起了这个错误:

object External {
  object MyMain extends App // Could not find or load main class in scala in intellij IDE

}

While the below unested worked:

虽然以下 unested 有效:

object External { }
object MyMain extends App // Worked!

回答by Tutu Kumari

Hi I solved this problem by defining the class in src package under main and by setting up the configuration as default.enter image description here

嗨,我通过在 main 下的 src 包中定义类并将配置设置为默认值来解决这个问题。在此处输入图片说明

回答by Ravinder Payal

Create a new project and make sure SBT is proper loaded along with Scala Library. This happens with many times, bad internet connection or failure of Scala library/SBT loading might be some of the reasons. Best of Luck for Next Project, Happy Developers & Coders.

创建一个新项目并确保 SBT 与 Scala 库一起正确加载。这种情况经常发生,一些原因可能是互联网连接不良或 Scala 库/SBT 加载失败。祝下一个项目好运,快乐的开发人员和编码人员。