我是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参数。将这些参数(只要可能)转换为环境变量为我解决了这个问题

其他回答

对我来说,我尝试了上面的几个选项,没有工作。然后我只是重命名了我的Application类,这可能迫使intelliJ构建一个新的jar和错误消息开始改变。然后我重新命名它,它工作。

In my case, the application runs well when code initially was under /src directory under the project. Later I added /main/java directory after the /src, then "Could not find or load main class ..." error happened. At that time you couldn't add /src/main/java/YourMain.java to the run configuration. What I did was to remove the run configuration, then right click the project->Open Module Settings (or F4 after click the Project panel)->Project Settings->Modules delete (x) the old Content Root and + Add Content Root, choose /src/main/java. Rebuild the project and it runs.

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

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

gradle clean build 

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