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

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


当前回答

无效缓存不起作用。

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

其他回答

在尝试了几乎每一个建议在这一步,我仍然不走运。在两台不同的机器上试过。请教一位有经验的同事。

最后:我的问题的解决方案,可能是问题的原因:

在我的mac电脑上,其中一个文件夹名称包含一个正斜杠,就是这样

(我们在共享驱动器上使用了(深度)嵌套的文件夹结构;我的同事在Windows上工作,对路径中的正斜杠没有问题)

注意:虽然我想在这个线程上提供我的答案,但我从stackoverflow得到了一个“警告”,那里已经有超过30个答案。但他,对我毫无用处。因为这是一个可以追溯到2012年的线索,在2023年仍然活跃,有很多答案(以及同一个问题的原因)。我决定在这里也加入我的50美分。希望它能帮助和节省一些人的时间!!

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.

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

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

gradle clean build 

我必须将“src”文件夹标记为“Sources”。在重新启动IntelliJ并重新构建项目后,我可以运行项目而没有进一步的问题(见截图)。 编辑:您可以通过File->项目结构或按Ctrl+Shift+Alt+S访问“项目结构”选项卡。