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

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


当前回答

文件>项目结构>模块>标记“src”文件夹为源。 这应该能解决问题。此外,检查最新的语言选择,以便您不必更改代码或做任何配置更改。

其他回答

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.

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

我遇到了这个问题,我尝试了我能想到的所有方法,在这个网站上。

从远程分支提取后,没有提取任何Java类。在项目层次结构中,所有类的名字都是红色的j,而不是蓝色的c。

最后,我尝试遵循本教程和几个步骤,尝试了一些没有描述的东西,并修复了问题: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html

以下是我所做的:

进入文件|项目结构,或按Crtl+Shift+Alt+S 在Project Settings部分下选择Modules。 在Sources选项卡中单击“Mark as:”行上的Sources。 单击Apply按钮。

因为某种原因,我所有的课都是蓝C。

对IntelliJ和/或IDE有更好理解的人可能能够解释这种现象,但我所知道的是,现在它可以看到所有的类,更重要的是主类,并运行。

对我来说,删除项目目录中的。idea文件工作正常!关闭ide, 删除项目中的.idea,打开项目构建并加载!

我从其他地方继承了一堆. java文件,不知道如何让它们在任何IDE中工作。最终,我不得不转到Main.JAVA文件所在的命令行,运行javac Main.JAVA。这创建了一堆. class文件。然后IDE就能够弄清楚要做什么。