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

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


当前回答

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

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

其他回答

在我的例子中,我配置了(相对)长的vm参数。将这些参数(只要可能)转换为环境变量为我解决了这个问题

遵循以下步骤

去跑步 点击编辑配置 点击左上角的绿色+符号 选择您正在工作的类型,例如“应用程序” 现在输入要运行的类的名称,而不是未命名的类 在写Main class的地方做同样的事情,就在它的下面。

好啊!…你的代码将运行:)

在我的案例中,在模块的构建中。gradle mainClassName赋值必须声明完全限定的类名,即。与主类源代码文件中指定的包名相同。

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

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

rm -rf .idea *.iml 

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