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

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


当前回答

如果具有主方法的类正在扩展/实现另一个类/接口,那么它将找不到主类。更改签名就可以了。

我也有同样的问题,几乎每个答案都试过了,但没有一个有效。这招对我很管用。

其他回答

简单的解决方案是在命令行中转到受支持的文件夹,其中是项目的根文件夹。您应该在您的机器中安装SBT。

如果你已经安装了sbt,请运行命令“sbt package”,这将重新编译项目,如果它成功,回到intelliJ它将工作。

在我的情况下,这是一个maven项目,我重新导入maven(右键单击pom.xml文件并单击重新导入),它立即工作。

Goto File->使缓存失效并重新启动。 否则删除rm -rf .idea *.iml 并重新启动InteliJ

如果以上答案都不适合你,只需关闭IntelliJ IDE,并从项目的根目录中删除IntelliJ IDE文件和文件夹:

rm -rf .idea *.iml 

然后用IntelliJ打开项目。它现在必须起作用。

对我来说,解决方案是在项目设置下修复输出目录。在此之前,我仅使用“target”作为Project编译器输出。相反,我将其更新为完整路径,例如D:\dev\sigplusjava2_68\target