Error: Could not find or load main class in intelliJ IDE

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I'm a beginner in Java and am trying to run my code using IntelliJ that I just installed as my IDE with JDK 1.7. The following piece of code keeps does not even compile and keeps giving me the error:

Error: Could not find or load main class libTest 

Code

import java.lang.Integer; import java.lang.String; import java.lang.System; import java.util.*;  class book {      private String name = "trial";     private int bookCode=1;     private int issued=0;       public void Issue(){          if(issued==0) {              issued=1;              System.out.println("You have succesfully issued the book");          }          else {              System.out.println("The book is already issued. Please contact the librarian for further details");          }     }      public int checkCode() {         return bookCode;     }      String readName() {         return name;     }      public void setName(String newName){         name=newName;     }      public void setBookCode(int newCode){         bookCode=newCode;     } }  class library {     private ArrayList books=new ArrayList();      public void getList(){         for(int bk:books){             String bName=books(bk).readName();             System.out.println((bk+1)+")  "+bName);         }     } }  public class libTest{     public static void main(String[] args){         library newLib= new library();         System.out.println("code working");    } } 

Is there any change that i have to make in the compiler settings?? Or is it the code.

回答1:

For me the solution was to fix the output directory under project settings. Before I was using just "target" for the Project compiler output. Instead I updated it to have a full path e.g. D:\dev\sigplusjava2_68\target



回答2:

This might help:

1) "Build" menu -> "Rebuild Project". Sometimes Intellij doesn't rewrite the classes because they already exist, this way you ask Intellij to rewrite everything.

2) "Run" menu -> "Edit configuration" -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu.

3)"Build" menu -> "Rebuild Project".



回答3:

If none of above answers didn't work for you, just close your intelliJ IDE and remove intelliJ IDE file and folder from root of project:

rm -rf .idea *.iml  

Then open project with intelliJ. It must work now.



回答4:

Explicitly creating an out folder and then setting the output path to C:\Users\USERNAME\IdeaProjects\PROJECTNAME\out

seemed to work for me when just out, and expecting IntelliJ to make the folder wouldn't.


Also try having IntelliJ make you a new run configuration:

Find the previous one by clicking

then remove it

and hit okay.

Now, (IMPORTANT STEP) open the class containing your main method. This is probably easiest done by clicking on the class name in the left-hand side Project Pane.

Give 'er a Alt + Shift + F10 and you should get a

Now hit Enter!!

Tadah?? (Did it work?)



回答5:

I know this was asked a while ago, but I was just stumbling over this issue and thought my findings might help others. As pointed out, the error message is basically a result of the out folder. That's because, when you're trying to run the program, it compiles the code first, and puts the compiled result to the out location, and then it tries to load the compiled code from the out location. If the compiled code is not in the location expected, you'll get the error.

The point I'm particularly wanting to share is that some times, the code is not compiled (built), even though your run configuration specifies "Build" in the "Before launch" section of the configuration panel.

When can this happen? One situation that can cause this to happen is if you're using modules and you manually delete the module out directory. For example, if I have a module named "foo", there should be a directory named foo under out/production. If you manually delete it, the build system may not know that it needs to be rebuilt.

Even worse, if you select Build | Build module 'foo', it still may not rebuild the module. If that's the case, you should select a file in the module, for example 'bar.java' and then select Build | Recompile 'bar.java'. Now the out directory out/production/foo should be restored.

Since IntelliJ typically knows about any changes going on, this surprised me, and took me a little time to figure out, so I thought I'd share.



回答6:

Check your class module : I have encountered this problem with intellij : I have a maven multi-module project, the problem is that i runing a class which not exist the module within the configuration, so my problem is fixed by setting the right module ("edit configuration" -> "use class of module")

may this help you



回答7:

I have faced such problems when the class is in the default folder, i.e. when the class does not declare a package.

So I guess using a package statement (eg. package org.me.mypackage;) on top of the class should fix it.



回答8:

Elaborating on Brad Turek's solution... One of the default IntelliJ Java project templates expects a file called Main defining the class Main and main() method entry point. If the method is contained in another file (and class), change the Run configuration:

Run Configuration Window

  1. With the project open in IntelliJ, use the Run:Edit Configurations... menu to open the build configuration window.
  2. If the entry for Main class doesn't list the name of your file containing the class exposing the main() entry method, enter the correct file name. (The configuration in the image is wrong, which is why it's red in the configuration panel.)
  3. My main() entry method is in the class (and file) ScrabbleTest. So changing Main class: to ScrabbleTest fixes the runtime error.

As others have noted you have to ReBuild using the new configuration. I am using a package, but that doesn't seem to make a difference IME. Hope this helps.



回答9:

In my case in the module's build.gradle the mainClassName assignment must state the fully qualified class name, ie. as specified as the package name in the main class' source code file.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!