首页 > 解决方案 > IntelliJ IDEA is not recognizing my java files as runnable and instead wants to run the class files. Any ideas of how to fix?

问题描述

Anytime I try to run a java file by clicking run, it instead tries to run the class file and thus gives a no main method error.

When I try to right click on a java file and run that, it doesn't recogize it as a file that is runable.

I am still able to run programs from the command prompt but I would prefer to be able to run them in IntelliJ.

This occurred after trying to create a Class path when using JSon Libraries.

Edit:

Normally it shows java files with this icon: enter image description here

But instead it's showing this icon for java files: enter image description here

I think the change in icon might have something to do with it.

Edit 2: the exact error I get is "C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java.exe"...bunch of other program file locations

Error: Could not find or load main class sample.Main

Process finished with exit code 1

标签: javaintellij-idea

解决方案


我遇到了同样的问题,发现我的 src 没有标记为 Sources Root。要改变这一点:

  • 鼠标右键单击您的 src
  • 导航到“将目录标记为”
  • 选择“Sources Root” 它现在可以正确识别您的文件,并且您的主类将能够再次运行

IntelliJ 无法识别我的 .java 文件

问题解决了


推荐阅读