4

I'm learning Java for my course and I've hit a brick wall. I've been tasked with developing a simple command line program. To make things easier I was given the following sample code to modify so I wouldn't 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'm using NetBeans and when I try to run the 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 get the program running so I can understand the code better. I understand the error, but have no idea where to implement the main method in this wall of text. I've been experimenting for hours, but obviously as a newbie I'm completely useless. Any help would be greatly appreciated.

user1410613
  • 41
  • 1
  • 1
  • 3
  • So ***where is*** the "main" method? (Also, try searching first :-/) –  May 22 '12 at 16:14
  • http://stackoverflow.com/questions/3696351/eclipse-java-no-main-method-found , http://stackoverflow.com/questions/5219306/why-a-java-program-should-have-a-main-method –  May 22 '12 at 16:15
  • I really don't know what was up with the edits... –  May 22 '12 at 16:22
  • I did search. I understood what the error was saying. I added a main method, but had no idea how to invoke the run() method through it without getting some other errors. I only started learning a few days ago (literally) so it's all new to me. Anyway, problem solved with Tudor's help. – user1410613 May 22 '12 at 16:33

7 Answers7

8

What you have currently is just a constructor named Main, what Java needs is a main method with exact signature as:

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

  • static - so that no need to create an instance of your class

  • void - not going to return any value

  • args - an array for command line parameters that you can specify while running the program

This is the entry point for your application.

When your current code is being invoked, JVM is trying to locate main method, and since its not present in your code, it's throwing the exception which you have received.

Since you have mentioned beginner in your post, its worth mentioning that Java is a case sensitive language - main and Main are not same in Java.

See also: The getting started tutorial.

Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122
mprabhat
  • 19,229
  • 7
  • 42
  • 62
6

The correct signature of main is:

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

It's even written in the error message you posted.

Remove the ; from the constructor:

public Main() {
    run();
}
Tudor
  • 58,972
  • 12
  • 89
  • 138
4

You have to use main() method in your program. From here the program execution starts.

like

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

This method must appear within a class, but it can be any class. In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application.

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

Aneesh Narayanan
  • 2,776
  • 11
  • 28
  • 44
2

It's looking for a method with this signature:

public static void main(String[] args)

To run your code, the main method can look like this:

public static void main(String[] args)
{
    new Main();
}
Ken Wayne VanderLinde
  • 17,085
  • 2
  • 42
  • 66
2

As the rather helpful error message states, you need a main method. See the java tutorials.

Qwerky
  • 17,564
  • 6
  • 43
  • 76
2

main method should exist for your application to run. Java applications need it to know where to begin executing the program.

Put the method in a class of your choice, and then right click file and select 'Run file`.

 public static void main(String[] args)
 {
     // your code here
 }
Josh Crozier
  • 202,159
  • 50
  • 343
  • 273
1

You need add a main method in your main class so that JVM will know where to start from and not a class with a "main" name.

public static void main(String[] args) {
   new Main();
}
GingerHead
  • 7,882
  • 14
  • 54
  • 91