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

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


当前回答

将该目录标记为源目录。通过Ctrl+Shift+Alt+S打开

其他回答

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

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

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

我们在文件/项目结构.. 答案可能是:

表示为“content root”的文件夹需要一个代码所在的子文件夹。 另外,找到将代码标记为排除和不排除的按钮。 不要与表示排除而不说明在哪个阶段和什么**的复选框混淆 是编译器排除还是运行时排除?你注定要接受考验和抽签。 所以没有那个复选框,只有图标和颜色。

作为一个想法,我们需要破解它最初被认为是如何工作的。他们从来没有把它放在第一位,并开始以过早的编码方式添加东西。 已经这么多年了,你不能指望有任何改善。 但作为治疗,我们可以想出一些方法,让它每次都正确。

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

希望这对你有所帮助

我在使用Scala/SBT时得到了这个错误。IntelliJ无法找到主类,尽管一切都设置正确。 我的解决方案是:删除<用户>/。sbt/<version>/plugins/目标文件夹,然后重启IntelliJ。