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

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


当前回答

这可能会有帮助:

1)“重建”菜单->“重建项目”。 有时Intellij不重写类,因为它们已经存在,这样你就可以让Intellij重写所有的东西。

2)“运行”菜单->“编辑配置”->删除配置文件->添加配置文件(如果是Java应用程序,则为“应用程序”),从“主类”下拉菜单中选择您的主类。

3)“重建”菜单->“重建项目”。

其他回答

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

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.

如果以上答案都不适合你,只需关闭IntelliJ IDE,并从项目的根目录中删除IntelliJ IDE文件和文件夹:

rm -rf .idea *.iml 

然后用IntelliJ打开项目。它现在必须起作用。

详细阐述布拉德·图雷克的解决方案……一个默认的IntelliJ Java项目模板需要一个名为Main的文件,该文件定义了类Main和Main()方法入口点。如果该方法包含在另一个文件(和类)中,则更改Run配置:

运行配置窗口

在IntelliJ中打开项目,使用Run:Edit Configurations… 菜单打开生成配置窗口。 类的条目没有列出包含公开 Main()输入方法,输入正确的文件名。(图片中的配置是错误的,这就是为什么它在配置面板中是红色的。) 我的main()入口方法在类(和文件)ScrabbleTest中。因此,将Main class:更改为ScrabbleTest可以修复运行时错误。

正如其他人所指出的,您必须使用新的配置重新构建。我正在使用一个包,但这似乎没有使差异输入法。希望这能有所帮助。

我必须将“src”文件夹标记为“Sources”。在重新启动IntelliJ并重新构建项目后,我可以运行项目而没有进一步的问题(见截图)。 编辑:您可以通过File->项目结构或按Ctrl+Shift+Alt+S访问“项目结构”选项卡。