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

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


当前回答

我几乎尝试了这里的答案中建议的所有方法,但没有一种对我有效。

在尝试运行应用程序一个小时之后,我注意到我的项目路径包括非ascii字符(阿拉伯字符)。在我将项目移动到没有非ascii字符的路径后,它执行得很好。

其他回答

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.

我在Spring中使用IntelliJ,我的主类被包装在JAR中。 我必须在运行/调试配置对话框中标记“包含具有“提供”范围的依赖项”

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

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

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

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

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

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

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

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