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

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


当前回答

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

其他回答

遵循以下步骤

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

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

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

gradle clean build 

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

检查你的类模块:我在intellij遇到了这个问题: 我有一个maven多模块项目,问题是我运行的类不存在的模块配置,所以我的问题是通过设置正确的模块(“编辑配置”->“使用模块类”)

希望这对你有所帮助

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