简体   繁体   中英

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: 在此处输入图片说明

But instead it's showing this icon for java files: 在此处输入图片说明

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

I had the same problem and found out that my src wasn't marked as Sources Root. To change this:

  • right-mouse click on your src
  • navigate to "Mark Directory As"
  • choose "Sources Root" It will now recognize your files correctly and your main classes will be able to run again

IntelliJ not recognizing my .java-files

Problem solved

我重新启动了我的电脑,并以某种方式修复了它。

I had a similar problem.It wouldn't pick up ModlServer in any project.
(Not just limited to the one I had open).

I had to file -> Manage IDE Settings -> Restore Default Settings


在此处输入图片说明

Note: I lost all my live templates, fonts, etc so just a heads up.

My issue was that I have multiple modules for my project and the module I was working on (where java files were not recognized) was not included in the main pom. I just added that module in the main (project) pom and it worked.

右键单击项目并单击“添加框架支持”,所有目录和类现在都应该是正确的类型。

If you are using Maven or Gradle, try to re-import the project and see if that helps. Could be that your IDEA project has some incorrect settings in it set by mistake, so this way those would be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM