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

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


当前回答

这可能会有帮助:

1)“重建”菜单->“重建项目”。 有时Intellij不重写类,因为它们已经存在,这样你就可以让Intellij重写所有的东西。

2)“运行”菜单->“编辑配置”->删除配置文件->添加配置文件(如果是Java应用程序,则为“应用程序”),从“主类”下拉菜单中选择您的主类。

3)“重建”菜单->“重建项目”。

其他回答

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

rm -rf .idea *.iml 

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

打开模块标签(按Ctrl+Shift+Alt+S)。我在一个项目下有两个模块。我在移除第二个冗余模块后解决了这个问题(见截图)。

当类在默认文件夹中,即类没有声明包时,我就遇到过这样的问题。

所以我猜使用包语句(例如。将org.me.mypackage;)打包到类顶部应该可以修复它。

当我的Java类在src/main/kotlin目录下时,我遇到了这个问题。当我把它移到src/main/java后,问题就不存在了。

使缓存无效并重新启动IntelliJ,这对我有用。