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

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


当前回答

我们在文件/项目结构.. 答案可能是:

表示为“content root”的文件夹需要一个代码所在的子文件夹。 另外,找到将代码标记为排除和不排除的按钮。 不要与表示排除而不说明在哪个阶段和什么**的复选框混淆 是编译器排除还是运行时排除?你注定要接受考验和抽签。 所以没有那个复选框,只有图标和颜色。

作为一个想法,我们需要破解它最初被认为是如何工作的。他们从来没有把它放在第一位,并开始以过早的编码方式添加东西。 已经这么多年了,你不能指望有任何改善。 但作为治疗,我们可以想出一些方法,让它每次都正确。

其他回答

只需重新构建项目(CTRL+F9),并再次检查(编辑配置),如果你有一个正确设置的主类和模块(如果有的话)

这可能会有帮助:

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

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

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

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.

在我的案例中,在模块的构建中。gradle mainClassName赋值必须声明完全限定的类名,即。与主类源代码文件中指定的包名相同。

遵循以下步骤

去跑步 点击编辑配置 点击左上角的绿色+符号 选择您正在工作的类型,例如“应用程序” 现在输入要运行的类的名称,而不是未命名的类 在写Main class的地方做同样的事情,就在它的下面。

好啊!…你的代码将运行:)