无法使用 Intellij IDEA 运行 Java 代码

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

Unable to run Java code with Intellij IDEA

javaintellij-idea

提问by Adam Griffiths

I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.

我刚刚下载了 IDE,我想用它编辑我的第一个 Java 文件,我对创建整个项目不感兴趣,只是编辑单个文件。

So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.

因此,我使用 Intellij IDEA 从桌面打开了该文件,因为我将其设置为打开 .java 文件的默认程序。

I write some code and the main run and debug buttons are greyed out! I can't run my code!

我写了一些代码,主运行和调试按钮变灰了!我无法运行我的代码!

I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!

我已经安装了 Java 8 更新 45 64 位(我有一个 64 位操作系统)以及 Java 开发工具包 (J8U45)。我已经将全局IDE SDK设置为我的JDK安装,当它提示我时我也将其设置为我的项目SDK,但仍然无法使用运行和调试按钮!

Edit: I am also unable to run my file regardless of if its in a project or not.

编辑:我也无法运行我的文件,无论它是否在项目中。

Edit 2: Screenshot of my project setup Project Setup

编辑 2:我的项目设置的屏幕截图 项目设置

采纳答案by Makoto

Move your code inside of the srcfolder. Once it's there, it'll be compiled on-the-fly every time it's saved.

将您的代码移动到src文件夹内。一旦它在那里,它就会在每次保存时即时编译。

IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.

IntelliJ 仅将特定位置的文件识别为项目的一部分——即,蓝色文件夹内的任何内容都被特别视​​为源代码。

Also - while I can't see allof your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a mainmethod (specifically public static void main(String[] args)). IntelliJ won't run code without a mainmethod (rather, it can't- neither it nor Java would know where to start).

另外 - 虽然我看不到你的所有源代码 - 确保它是正确的 Java 语法,声明的类与文件相同,并且它有一个main方法(特别是public static void main(String[] args))。IntelliJ 不会在没有main方法的情况下运行代码(相反,它不能——它和 Java 都不知道从哪里开始)。

回答by Ken Ratanachai S.

Don't forget the String[] argsin your main method. Otherwise, there's no option to run your program:

不要忘记String[] argsmain 方法中的 。否则,没有选项可以运行您的程序:

public static void main(String[] args) {

}

回答by MamathaMacherla

My classes contained a main()method yet I was unable to see the Runoption. That option was enabled once I marked a folder containing my class files as a source folder:

我的类包含一个main()方法,但我无法看到Run选项。一旦我将包含我的类文件的文件夹标记为源文件夹,就启用了该选项:

  1. Right click the folder containing your source
  2. Select Mark Directory as → Test Source Root
  1. 右键单击包含源的文件夹
  2. 选择将目录标记为 → 测试源根

Some of the classes in my folder don't have a main()method, but I still see a Run option for those.

我的文件夹中的某些类没有main()方法,但我仍然看到这些类的运行选项。

回答by Niraj Kulkarni

If you are just opened a new java project then create a new folder src/in the man project location.

如果您刚刚打开一个新的 java 项目,则在 man 项目位置创建一个新文件夹 src/

Then cut and paste all your package in that folder.

然后将所有包剪切并粘贴到该文件夹​​中。

Then Right click on src directory and select option Mark Directory As > Sources Root.

然后右键单击 src 目录并选择选项 Mark Directory As > Sources Root

回答by Matthew E

right click on the "SRC folder", select "Mark directory as:, select "Resource Root".

右键单击“SRC文件夹”,选择“标记目录为:”,选择“资源根目录”。

Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.

然后编辑运行配置。选择运行,运行,编辑配置,用加号按钮添加一个应用程序配置,给它起个名字(可以是任何名字),并在主类中写下主java类的全名,例如com.example。 java.MaxValues。

you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,

您可能还需要检查文件、项目结构、项目设置-项目,给它一个编译器输出的文件夹,最好是一个单独的文件夹,在 java 文件夹下,

回答by HappySpaceBoy

Something else that worked for me:

其他对我有用的东西:

  1. Right click the folder in src containing your main
  2. You'll see an option "run 'file.main()'" with the run icon.
  3. Click it, and then the run icon in the top right and bottom left will turn green from then on.
  1. 右键单击 src 中包含主文件的文件夹
  2. 您将看到带有运行图标的选项“run 'file.main()'”。
  3. 单击它,然后右上角和左下角的运行图标将变为绿色。

回答by Arnaud Claudel

Sometimes, patience is key.

有时,耐心是关键。

I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.

我在一个带有大 node_modules / .m2 目录的 java 项目中遇到了同样的问题。
索引很长,所以我暂停了它,它阻止了我使用运行配置。

So I waited for the indexing to finish and only then I was able to run my main class.

所以我等待索引完成,然后我才能运行我的主类。

回答by Dipesh Chauhan

If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:

如果您无法运行正确的程序并尝试所有其他答案。单击“编辑配置”并执行以下步骤:

  1. Click on add icon and select Application from the list.
  2. In configuration name your Main class: as your main class name.
  3. Set working directory to your project directory.
  4. Others: leave them default and click on apply. Now you can run your program.enter image description here
  1. 单击添加图标并从列表中选择应用程序。
  2. 在配置名称中您的主类:作为您的主类名称。
  3. 将工作目录设置为您的项目目录。
  4. 其他:保留默认值,然后单击应用。现在你可以运行你的程序了。在此处输入图片说明

回答by U?ur Abdio?lu

If you use Maven, you must to declare your source and test folder in project directory.

如果使用 Maven,则必须在项目目录中声明源和测试文件夹。

For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.

对于这些,单击 Intellij Idea 上的 F4 并打开项目结构。在左侧面板上选择您的项目名称并标记为您的“源”和“测试”目录。

回答by Roy

I had the similar issue and solved it by doing the below step.

我遇到了类似的问题并通过执行以下步骤解决了它。

  1. Go to "Run" menu and "Edit configuration"
  2. Click on add(+) icon and select Application from the list.
  3. In configuration name your Main class: name of your main class.
  4. Working Directory : It should point till the src folder of your project.C:\Users\name\Work\ProjectName\src

    This is where I had issue and after correcting this, I could see the run option for that class.

  1. 转到“运行”菜单和“编辑配置”
  2. 单击添加(+) 图标并从列表中选择应用程序。
  3. 在配置名称中您的主类:您的主类的名称。
  4. 工作目录:它应该指向项目的 src 文件夹。C:\Users\name\Work\ProjectName\src

    这是我遇到问题的地方,更正后,我可以看到该类的运行选项。