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

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


当前回答

遵循以下步骤

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

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

其他回答

modules.xml有错误的内容,我不知道我的IDEA有什么问题。

详细阐述布拉德·图雷克的解决方案……一个默认的IntelliJ Java项目模板需要一个名为Main的文件,该文件定义了类Main和Main()方法入口点。如果该方法包含在另一个文件(和类)中,则更改Run配置:

运行配置窗口

在IntelliJ中打开项目,使用Run:Edit Configurations… 菜单打开生成配置窗口。 类的条目没有列出包含公开 Main()输入方法,输入正确的文件名。(图片中的配置是错误的,这就是为什么它在配置面板中是红色的。) 我的main()入口方法在类(和文件)ScrabbleTest中。因此,将Main class:更改为ScrabbleTest可以修复运行时错误。

正如其他人所指出的,您必须使用新的配置重新构建。我正在使用一个包,但这似乎没有使差异输入法。希望这能有所帮助。

您可能指定了一个错误的包,并且包的层次结构也不正确。看下面

在这种情况下,ide会突出显示错误的路径。

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

无效缓存不起作用。

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