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

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


当前回答

你可以在项目目录下的pom.xml文件上运行maven命令:

MVN清洁安装

其他回答

在我的情况下,它有助于删除线条build.gradle

编译组:com.microsoft。Sqlserver ',名称:'mssql-jdbc',版本:'8.2.0.jre11'

我不知道为什么会发生这种情况,但是在添加指定的库之后,编译的jar停止工作

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

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

rm -rf .idea *.iml 

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

当我去“编辑配置”->“修改选项”并勾选“在运行前不构建”选项时,我得到了这个问题,因为我使用IntelliJ来解决简短的编程问题,我不希望它每次都运行构建。

相反,我所做的是分配一个运行构建过程的快捷方式(仅在创建类之后第一次),这修复了错误,然后另一个方便的快捷方式“运行上下文配置”,成功地运行当前关注的任何类中的主方法。

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

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