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

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


当前回答

如果您正在使用gradle,请尝试从命令行通过命令重建项目:

gradle clean build 

其他回答

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

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.

简单的解决方案是在命令行中转到受支持的文件夹,其中是项目的根文件夹。您应该在您的机器中安装SBT。

如果你已经安装了sbt,请运行命令“sbt package”,这将重新编译项目,如果它成功,回到intelliJ它将工作。

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

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

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

-DsomeArgument="arg with space must be quoted"