简体   繁体   English

我一直收到错误消息“找不到或加载主类gradebooktest.GradeBookTest”,我不确定为什么

[英]I keep getting an error saying “Could not find or load main class gradebooktest.GradeBookTest” I am not sure why

I am using netbeans and my code is as follows 我正在使用netbeans,我的代码如下

public class GradeBook
{
    private String courseName; // course name for this GradeBook
    private String courseInstructor; // instructor name for this GradeBook 

// constructor initializes courseName and courseInstructor with String Argument
    public GradeBook( String name, String insname ) // constructor name is class name
    {
        courseName = name; // initializes courseName
        courseInstructor = insname; // initializes courseInstructor 
    } // end constructor

    // method to set the course name
    public void setCourseName( String name )
    {
        courseName = name; // store the course name
    } // end method setCourse

    // method to retrieve the course name
    public String getCourseName()
    {
        return courseName;
    } // end method getCourseName

    // method to set the Instructor name 
    public void setInstructorName( String insname)
    { 
        courseInstructor = insname; // store the Instructor name 
    } // end method setInstructorName 

    // method to retrieve the Instructor name 
    public String getInstructorName()
    { 
        return courseInstructor; 
    } // end method getInstructorName 

    // display a welcome message to the GradeBook user
    public void displayMessage()
    {
        // this statement calls getCourseName to get the 
        // name of the course this GradeBook represents
        System.out.println( "\nWelcome to the grade book for: \n"+
        getCourseName()+"\nThis course is presented by: "+getInstructorName()); 
        System.out.println( "\nProgrammed by Jack Friedman");

    } // end method displayMessage
} // end

You should call the constructor of this class in your main method. 您应该在main方法中调用此类的构造函数。

Make a new class GradeBookTest as follows: 新建一个新的GradeBookTest类,如下所示:

public class GradeBookTest {

  public static void main (String args[]) {
     GradeBook book = new GradeBook("Math", "T.I.");
    book.displayMessage(); //To see your results
   }

}

Now you can launch this class to view your results. 现在,您可以启动此类以查看您的结果。

请向您的应用程序添加静态main方法。

Where is the main method? 主要方法在哪里? Include below code to run your program- 包括以下代码以运行您的程序-

public static void main(String[] args) {
      GradeBook book = new GradeBook("subj1", "instruc1");
      book.displayMessage();
    }

In the Java programming language, every application must contain a main method whose signature is: 在Java编程语言中,每个应用程序都必须包含一个主要方法,其签名为:

public static void main(String[] args)

main method is the entry point for a java program, it has to be declared as public so that it can be accessed from outside the class and static so that it can be accessed even without creating an instance or object of the class. main方法是Java程序的入口点,必须将其声明为public以便可以从类外部访问它,而可以将其声明为static以便即使不创建类的实例或对象也可以对其进行访问。

For better understanding please read this 为了更好的理解,请阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我找不到或加载主类错误? - Why am I getting could not find or load main class error? 为什么在NetBeans中出现“无法找到或加载主类……”错误? - Why am I getting “Could not find or load main class …” error in NetBeans? 在Eclipse中,为什么会出现错误“错误:找不到或加载主类Coordinator.java”? - In Eclipse, why am I getting the error “Error: Could not find or load main class Coordinator.java”? 我不断收到错误消息:无法找到或加载主类,无法找出原因? - I keep getting Error: Could not find or load main class and cant figure out why? 我不断收到此错误错误:无法找到或加载主类javaclass.Javaclass - i keep getting this error Error: Could not find or load main class javaclass.Javaclass Java我在cdm提示符Windows 7中收到所有内容的“错误:无法找到或加载主类” - Java I am getting an “Error: Could not find or load main class” for everything in cdm prompt Windows 7 我正在尝试让 JFrame 工作,但它不断弹出错误消息“无法找到或加载主类” - I'm trying to get JFrame to work, but it keeps popping up with a error saying "Could not find or load main class" Java: 无法找到或加载 main class main - 不知道为什么 - Java: could not find or load main class main - not sure why 在 Windows 中的 run.batch 中运行 jar 文件,我无法找到或加载主类 - Running a jar file in run.batch in windows i am getting could not find or load the main class 无法在Eclipse中使用Gradle运行黄瓜功能文件。 我收到“错误:无法找到或加载主类Cucumber.api.cli.Main”错误 - Unable to run cucumber feature file using gradle in Eclipse. I am getting “Error: Could not find or load main class cucumber.api.cli.Main” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM