Error "Basic method not found" when starting the program?

I am learning Java for my course and I hit a brick wall. I was tasked with developing a simple command line program. To simplify the task, I was provided with the following sample code to modify, so I would not have to start from scratch.

package assignment; public class Main { private final static String[] mainMenuOpts = {"Students","Lecturers","Admin","Exit"}; private final static String[] studentMenuOpts = {"Add Student","List all Students","Find a Student","Return to Main Menu"}; private Menu mainMenu = new Menu("MAIN MENU",mainMenuOpts); private Menu studentMenu = new Menu("STUDENT MENU",studentMenuOpts); private DataStore data = new DataStore(); private java.io.PrintStream out = System.out; private ReadKb reader = new ReadKb(); /** Creates a new instance of Main */ public Main() { run(); } private void run(){ int ret = mainMenu.display(); while(true){ switch(ret){ case 1: students();break; case 2: lecturers(); break; case 3: admin(); break; case 4: exit(); break; } ret = mainMenu.display(); } } private void students(){ int ret = studentMenu.display(); while(ret != 4){ switch(ret){ case 1: addStudent();break; case 2: listStudents(); break; case 3: findStudent(); break; } ret = studentMenu.display(); } } private void lecturers(){ out.println("\nLecturers not yet implemented"); } private void admin(){ out.println("\nAdmin not yet implemented"); } //Student methods private void addStudent(){ out.println("\n\tAdd New Student"); //prompt for details //add student to the datastore //ask if they want to enter another student - // if so call addStudent again //otherwise the method completes and the studentMenu will display again } private void listStudents(){ out.println("\n\tStudent Listing"); //list all students from the datastore } private void findStudent(){ out.println("\n\tFind Student"); out.print("Enter Search String: "); //reasd search text //use datastore method to get list of students that contain the search string //display matching students } // end Student methods private void exit() { data.save(); //call the datastore method that will save to file out.println("\n\nGoodbye :)"); System.exit(0); } } 

I am using NetBeans, and when I try to start a project, I get this error:

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

I just want to run the program to better understand the code. I understand the error, but I have no idea where to implement the main method in this text wall. I experimented for hours, but obviously, as a beginner, I am completely useless. Any help would be greatly appreciated.

+4
source share
7 answers

What you currently have is just a constructor named Main, what Java requires is a basic method with an exact signature like:

 public static void main(String[] args) 
  • public - so that it can be called from outside

  • static - so there is no need to instantiate your class

  • void - do not return any value

  • args - an array of command line options that you can specify when starting the program

This is the entry point for your application.

When your current code is called, the JVM tries to find the main method, and since it is not present in your code, it throws an exception.

Since you mentioned the newcomer in your post, it is worth mentioning that Java is a case-sensitive language - main and main , not like Java.

See also: Getting started tutorial .

+7
source

The correct main signature is:

 public static void main(String[] args) { new Main(); } 

It is even written in the error message you posted.

Remove ; from the constructor:

 public Main() { run(); } 
+6
source

You must use the main () method in your program. This is where the program starts.

like

 public static void main(String args[]) { //This is the starting point of your program. } 

This method should be displayed inside the class, but can be any class. In Java, when you execute a class using the Java interpreter, the runtime system is started using the main () class method. The main () method then calls all the other methods needed to launch your application.

The main () method takes a single parameter: an array of strings. This parameter is the mechanism by which the runtime system passes command line arguments to your application.

+4
source

He is looking for a method with this signature:

 public static void main(String[] args) 

To run the code, the main method may look like this:

 public static void main(String[] args) { new Main(); } 
+2
source

As a very useful error message, you need a basic method. See java tutorials .

+2
source

main must exist to run your application. Java applications need to know where to start program execution.

Place the method in the class of your choice, and then right-click the file and select "Run File".

  public static void main(String[] args) { // your code here } 
+2
source

You need to add the main method to your main class so that the JVM knows where to start, and not with the class with the "main" name.

 public static void main(String[] args) { new Main(); } 
+1
source

Source: https://habr.com/ru/post/1413801/


All Articles