user3132936 user3132936 - 2 months ago 14
Java Question

Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args)

I try compiling it and it says no error. But when i run the program it says:


Error: Main method not found in class TextBook, please define the main
method as: public static void main(String[] args)


so when i add the
public static void main
, the whole program has so many errors.

import java.util.*;

public class Book

{
private int pageNum;
private String title;
Date today = new Date();

public Book(int pn, String name)
{
pageNum = pn;
title = name;
}

//Setter/Getter for pageNum
public int getpageNum()
{
return pageNum;
}

public void setpageNum(int pn)
{
pageNum = pn;
}
//Setter/Getter for Title
public String getTitle()
{
return title;
}

public void setTitle(String name)
{
title = name;
}

public void display()
{
System.out.println("Book.java" + "\nby Tyler " + today);
System.out.println("Book Title: " + title + "\nNumber of Pages: " + pageNum);
}
}


for TextBook.java

public class TextBook extends Book
{
private String gradeLevel;

public TextBook(int pageNum, String title, String gl)
{
super(pageNum, title);
gradeLevel = gl;
}

//Setter/Getter for gradeLevel
public String getGradeLevel()
{
return gradeLevel;
}

public void setGradeLevel(String g)
{
gradeLevel = g;
}

public void display()
{
super.display();
System.out.println("Grade Level: " + gradeLevel);
}

}


for DemoBook.java

public class DemoBook
{
public static void main(String[] args)
{
Book oneBook = new Book(250, "Awesome Story");
TextBook oneTextBook = new TextBook(350, "Awesomer Story", "12");

oneBook.display();
oneTextBook.display();
}
}

Answer

You can only run java file with main method.

In your case, you can only run DemoBook.java

Comments