Java 无法在 IntelliJ 中选择类作为主类

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

Can't choose class as main class in IntelliJ

javaintellij-idea

提问by C. E.

I have a Java project in IntelliJ to which I just added a bunch of files in a nested folder hierarchy. Many of these files are tests and include the main method, so I should be able to run them. However I cannot figure out how to do this.

我在 IntelliJ 中有一个 Java 项目,我只是在嵌套文件夹层次结构中添加了一堆文件。这些文件中有许多是测试文件并包含 main 方法,所以我应该能够运行它们。但是我无法弄清楚如何做到这一点。

I'm new to IntelliJ and previously files have shown up in the project hierarchy with the symbolthat corresponds to "Java class that contains declaration of the main() method." but in this scenario they show up with the symbol corresponding to "Java class located out of the source root."

我是 IntelliJ 的新手,以前的文件已显示在项目层次结构中,其符号对应于“包含 main() 方法声明的 Java 类”。但在这种情况下,它们显示为对应于“位于源根之外的 Java 类”的符号。

So how do I compile and run these files?

那么如何编译和运行这些文件呢?

采纳答案by trappski

The documentation you linked actually has the answer in the link associated with the "Java class located out of the source root." Configure your source and test roots and it should work.

您链接的文档实际上在与“位于源根目录之外的 Java 类”相关联的链接中提供了答案。配置您的源和测试根,它应该可以工作。

https://www.jetbrains.com/idea/webhelp/configuring-content-roots.html

https://www.jetbrains.com/idea/webhelp/configuring-content-roots.html

Since you stated that these are tests you should probably go with them marked as Test Source Root instead of Source Root.

由于您声明这些是测试,您可能应该将它们标记为测试源根而不是源根。

回答by JB Nizet

Select the folder containing the package tree of these classes, right-click and choose "Mark Directory as -> Source Root"

选择包含这些类的包树的文件夹,右击选择“Mark Directory as -> Source Root”

回答by Ashwin

Here is the complete procedure for IDEA IntelliJ 2019.3:

以下是 IDEA IntelliJ 2019.3 的完整过程:

  1. File > Project Structure

  2. Under Project Settings > Modules

  3. Under 'Sources' tab, right-click on 'src' folder and select 'Sources'.

  4. Apply changes.

  1. 文件 > 项目结构

  2. 在项目设置 > 模块下

  3. 在“Sources”选项卡下,右键单击“src”文件夹并选择“Sources”。

  4. 应用更改。