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

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


当前回答

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

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

其他回答

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

您可能指定了一个错误的包,并且包的层次结构也不正确。看下面

在这种情况下,ide会突出显示错误的路径。

遵循以下步骤

去跑步 点击编辑配置 点击左上角的绿色+符号 选择您正在工作的类型,例如“应用程序” 现在输入要运行的类的名称,而不是未命名的类 在写Main class的地方做同样的事情,就在它的下面。

好啊!…你的代码将运行:)

对我来说,解决方案是在项目设置下修复输出目录。在此之前,我仅使用“target”作为Project编译器输出。相反,我将其更新为完整路径,例如D:\dev\sigplusjava2_68\target

I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main. I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings. After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects. Thanks to Konstantin at JetBrain's support for his patience.