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

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


当前回答

我从其他地方继承了一堆. java文件,不知道如何让它们在任何IDE中工作。最终,我不得不转到Main.JAVA文件所在的命令行,运行javac Main.JAVA。这创建了一堆. class文件。然后IDE就能够弄清楚要做什么。

其他回答

对我来说,删除项目目录中的。idea文件工作正常!关闭ide, 删除项目中的.idea,打开项目构建并加载!

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

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.

尝试检查您的项目名称和完整目录名称,看看是否包含任何“非英语”单词(如亚洲单词),有时这可能是错误的。重命名项目名称或项目路径包含这样的单词并重新打开它可能有效。

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