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

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


当前回答

这里可以检查的另一件事是传递给JVM的实际命令,并确保它看起来正常。滚动到Run控制台的顶部,它应该是第一行。

“运行配置虚拟机选项”字段中的空格将使应用程序启动命令格式不正确,并可能导致此错误消息

-DsomeArgument="arg with space must be quoted"

其他回答

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.

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

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

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

我知道之前有人问过这个问题,但我只是被这个问题绊倒了,我想我的发现可能会帮助到其他人。正如所指出的,错误消息基本上是out文件夹的结果。这是因为,当您试图运行程序时,它首先编译代码,并将编译后的结果放到输出位置,然后它尝试从输出位置加载编译后的代码。如果编译后的代码不在预期的位置,就会得到错误。

我特别想要分享的一点是,有些时候,代码没有编译(构建),即使您的运行配置在配置面板的“Before launch”部分指定了“构建”。

这种情况什么时候会发生? 可能导致这种情况发生的一种情况是,如果您正在使用模块,并手动删除模块的目录。例如,如果我有一个名为“foo”的模块,那么在out/production下应该有一个名为foo的目录。如果手动删除它,构建系统可能不知道需要重新构建它。

更糟糕的是,如果你选择Build | Build module 'foo',它仍然可能无法重建模块。如果是这种情况,您应该在模块中选择一个文件,例如'bar.java',然后选择Build | Recompile 'bar.java'。现在应该恢复out目录out/production/foo。

由于IntelliJ通常知道发生的任何变化,这让我感到惊讶,并花了一些时间来弄清楚,所以我想分享一下。

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

rm -rf .idea *.iml 

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