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

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


当前回答

In my case, the application runs well when code initially was under /src directory under the project. Later I added /main/java directory after the /src, then "Could not find or load main class ..." error happened. At that time you couldn't add /src/main/java/YourMain.java to the run configuration. What I did was to remove the run configuration, then right click the project->Open Module Settings (or F4 after click the Project panel)->Project Settings->Modules delete (x) the old Content Root and + Add Content Root, choose /src/main/java. Rebuild the project and it runs.

其他回答

使缓存无效并重新启动IntelliJ,这对我有用。

当类在默认文件夹中,即类没有声明包时,我就遇到过这样的问题。

所以我猜使用包语句(例如。将org.me.mypackage;)打包到类顶部应该可以修复它。

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

我遇到同样的问题,用以下方法解决:

关闭所有IDEA窗口 删除C:/Userz/xx/.IdeaIC2019.3文件夹 开放的想法

我在Spring中使用IntelliJ,我的主类被包装在JAR中。 我必须在运行/调试配置对话框中标记“包含具有“提供”范围的依赖项”