我是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类。在项目层次结构中,所有类的名字都是红色的j,而不是蓝色的c。

最后,我尝试遵循本教程和几个步骤,尝试了一些没有描述的东西,并修复了问题: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html

以下是我所做的:

进入文件|项目结构,或按Crtl+Shift+Alt+S 在Project Settings部分下选择Modules。 在Sources选项卡中单击“Mark as:”行上的Sources。 单击Apply按钮。

因为某种原因,我所有的课都是蓝C。

对IntelliJ和/或IDE有更好理解的人可能能够解释这种现象,但我所知道的是,现在它可以看到所有的类,更重要的是主类,并运行。

其他回答

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

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

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

什么都试过了,运气不佳。最后,对我来说(在Mac上)有效的简单解决方案是:

当项目处于打开状态时,转到查找器并将SRC目录移出项目文件夹。IntelliJ意识到src不再存在,并更新自己。 在项目仍然打开的情况下,将SRC目录移回原位。IntelliJ更新了它的项目配置,此时一切正常。

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.

如果具有主方法的类正在扩展/实现另一个类/接口,那么它将找不到主类。更改签名就可以了。

我也有同样的问题,几乎每个答案都试过了,但没有一个有效。这招对我很管用。