0

Created a file Server.java which will run on a System and will wait for connection request(in an infinite loop). This file in Server.java has main funciton defined with correct signature but still I am getting following error.

Error: Main method not found in class Server, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

    class Server {   
    final static int TRACKER_PORT = 5000;
    public static void main(String args[]) {   
        try (ServerSocket s = new ServerSocket(TRACKER_PORT)) {
        System.out.println("The tracker server is running on port " + TRACKER_PORT);
        Tracker t = new Tracker();
        while(true) {
            Socket cl = s.accept();
            new Handler(cl,t);
        }

    }   
    catch(Exception e) {
        System.out.println("Unable to open socket");
    }   
}

Dear All, Thank you for your time and efforts. Above problems has been solved. It occured because a Server.class also existed at one of the path I set as CLASSPATH. so compiler was looking for main function in that file and that file doesn't had any main file. I re-run the code after removing that CLASSPATH and now code is running fine.

Sumit Rao
  • 1
  • 1
  • Does this answer your question? ["Error: Main method not found in class MyClass, please define the main method as..."](https://stackoverflow.com/questions/5407250/error-main-method-not-found-in-class-myclass-please-define-the-main-method-as) – flaxel Sep 19 '20 at 20:36
  • Do you have, by any chance, a class named `String`?. – Johannes Kuhn Sep 19 '20 at 20:40
  • 1
    Dear All, Thank you for your time and efforts. Above problems has been solved. It occured because a Server.class also existed at one of the path I set as CLASSPATH. so compiler was looking for main function in that file and that file doesn't had any main file. I re-run the code after removing that CLASSPATH and now code is running fine. – Sumit Rao Sep 19 '20 at 20:42

2 Answers2

1

The main method is missing curly braces.

class Server {
    final static int TRACKER_PORT = 5000;
    public static void main(String args[]) {
        try(ServerSocket s = new ServerSocket(TRACKER_PORT)) {
            System.out.println("The tracker server is running on port " + TRACKER_PORT);
            Tracker t = new Tracker();
            while(true) {
                Socket cl = s.accept();
                new Handler(cl, t);
            }

        } catch(Exception e) {
            System.out.println("Unable to open socket");
        }
    }
}
Biel675
  • 11
  • 1
-1

i think you have to make server class as public.