我是Java初学者,正在尝试使用IntelliJ运行我的代码,我刚刚将它安装为JDK 1.7的IDE。下面这段代码甚至不能编译,并不断给我错误:

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");
   }
}

有任何改变,我必须在编译器设置??或者是代码。


当前回答

I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main. I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings. After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects. Thanks to Konstantin at JetBrain's support for his patience.

其他回答

我在使用Scala/SBT时得到了这个错误。IntelliJ无法找到主类,尽管一切都设置正确。 我的解决方案是:删除<用户>/。sbt/<version>/plugins/目标文件夹,然后重启IntelliJ。

如果您正在使用gradle,请尝试从命令行通过命令重建项目:

gradle clean build 

检查你的类模块:我在intellij遇到了这个问题: 我有一个maven多模块项目,问题是我运行的类不存在的模块配置,所以我的问题是通过设置正确的模块(“编辑配置”->“使用模块类”)

希望这对你有所帮助

我已经尝试了这里建议的所有方法,但都无济于事。最后,我简单地创建了一个新的Maven应用程序,并逐个手动地将pom.xml和java文件和资源复制到其中。现在一切都正常了。我是IntelliJ的新手,完全没有印象,但它很容易进入不稳定的状态。

无效缓存不起作用。

我编辑了主类java文件,做了一个虚假的更改并运行它。它工作。