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

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


当前回答

在我的例子中,我配置了(相对)长的vm参数。将这些参数(只要可能)转换为环境变量为我解决了这个问题

其他回答

只需重新构建项目(CTRL+F9),并再次检查(编辑配置),如果你有一个正确设置的主类和模块(如果有的话)

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

我几乎尝试了这里的答案中建议的所有方法,但没有一种对我有效。

在尝试运行应用程序一个小时之后,我注意到我的项目路径包括非ascii字符(阿拉伯字符)。在我将项目移动到没有非ascii字符的路径后,它执行得很好。

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

希望这对你有所帮助

In my case the problem seemed to be related to upgrading IntelliJ. When I did this I overwrote the files from the old IntelliJ with the files from the new IntelliJ (2017 community to 2018 community). After that all of my projects were broken. I tried everything in this thread and none of them worked. I tried upgrading gradle to the latest version (4 to 4.8) and that didn't work. The only thing that worked for me was deleting the entire IntelliJ folder and reinstalling it. All of my projects worked after that.