0
votes

I am newbie in java networking ,I am trying to make a server application in java called instant messaging without GUI. So whenever i run the program it says " Main method not found in class Methods, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application" -ECLIPSE

Please help me what's wrong in my code.

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

// FIRST FILE //
public class main{

    public static void main (String[] args) {
    Server s=new Server();
    s.runningserver();

    }
}   

//SECOND FILE//
public class Server {

    private ObjectOutputStream output;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;

    //wait For Connection,then display connection information
            private void waitforConnection() {
                System.out.println("wait for Someone to Connect.....\n");
                try {
                    connection=server.accept();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("error In Acceptance of Server!!\n...");
                }
            System.out.println(("Connection      Established"+connection.getInetAddress().getHostName()));

            }


            //Setting Up Streams to Get Input and Output

            private void setupStreams(){  
                try {
                    output=new ObjectOutputStream(connection.getOutputStream());
                    output.flush();
                    input=new ObjectInputStream(connection.getInputStream());
                System.out.println("Your Streams are Perfectly Working...");


                } catch (IOException e) {
                    // TODO Auto-generated catch block

                    System.err.println("Error Found! in Streaming Connectionos");
                    e.printStackTrace();
                }
            }

            // While Chatting Method....!!//
                private void whileChatting() throws IOException{
               String Message="You are now Connected!!\n";
               sendMessage(Message);

               //abletoType(true);
               do{
                   try{
                   Message=(String) input.readObject();
                   System.out.println("\n"+Message);
               }       
                catch(ClassNotFoundException e ){
                    System.out.println("wtf---Fuck\n YOu\n Bloody\n HAcker!!!\n");
                }



               }
                  while(!Message.equals("CLIENT--END"));

                    }
                // Closing All Streams and Socket after you are done//
                private void closeCrap(){
                    System.out.println("\nClosing Connection...........Bye Bye\n");
                    //abletoType(false);
                    try {
                        output.close();
                        input.close();
                        connection.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
        System.out.println("Couldn't Close Connections!");
                    }

                }
                //Sending Messages to Client
                private void sendMessage(String message){
                    try {

                        output.writeObject("SERVER--- "+message);

                        output.flush();
            System.out.println("\nServer- "+message);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block




                    e.printStackTrace();
        System.out.println("Dude I cant Send yeaah..");
                    }

                }






    // Setting up the Server

public void runningserver(){
        try {
            server=new ServerSocket(4444,100);
            while(true){try{
                //connect and Have connection
                waitforConnection();
                setupStreams();
                whileChatting();
            }
            finally{
                closeCrap();
            }
            }   
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        }




    }
2
Which class are you trying to run? Server or main? - M. Deinum
@M.Deinum main Class. Plus I had tried both of them too,its still buggy.bro - M.Usman Siddiqui
is it even possible to have two public classes in the same file? How do you compile it? And what exactly is the command line you are running it with? - Dima
The error message suggests you are trying to run a class called "Methods" not "main" or "Server". What is the command line you are running? - Paolo
change ur public class server to class server, that should resolve it - nafas

2 Answers

2
votes

EDIT: Here is the solution and I checked it and for me it works (run the MainClass.java file!): //MainClass.java file:

public class MainClass {
    public static void main (String[] args) {
    Server s=new Server();
    s.runningserver();
    } 
}

//Server.java file:

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private ObjectOutputStream output;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;

    // wait For Connection,then display connection information
    private void waitforConnection() {
        System.out.println("wait for Someone to Connect.....\n");
        try {
            connection = server.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("error In Acceptance of Server!!\n...");
        }
        System.out.println(("Connection      Established" + connection
                .getInetAddress().getHostName()));

    }

    // Setting Up Streams to Get Input and Output

    private void setupStreams() {
        try {
            output = new ObjectOutputStream(connection.getOutputStream());
            output.flush();
            input = new ObjectInputStream(connection.getInputStream());
            System.out.println("Your Streams are Perfectly Working...");

        } catch (IOException e) {
            // TODO Auto-generated catch block

            System.err.println("Error Found! in Streaming Connectionos");
            e.printStackTrace();
        }
    }

    // While Chatting Method....!!//
    private void whileChatting() throws IOException {
        String Message = "You are now Connected!!\n";
        sendMessage(Message);

        // abletoType(true);
        do {
            try {
                Message = (String) input.readObject();
                System.out.println("\n" + Message);
            } catch (ClassNotFoundException e) {
                System.out.println("wtf---Fuck\n YOu\n Bloody\n HAcker!!!\n");
            }

        } while (!Message.equals("CLIENT--END"));

    }

    // Closing All Streams and Socket after you are done//
    private void closeCrap() {
        System.out.println("\nClosing Connection...........Bye Bye\n");
        // abletoType(false);
        try {
            output.close();
            input.close();
            connection.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Couldn't Close Connections!");
        }

    }

    // Sending Messages to Client
    private void sendMessage(String message) {
        try {

            output.writeObject("SERVER--- " + message);

            output.flush();
            System.out.println("\nServer- " + message);

        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
            System.out.println("Dude I cant Send yeaah..");
        }

    }

    // Setting up the Server

    public void runningserver() {
        try {
            server = new ServerSocket(4444, 100);
            while (true) {
                try {
                    // connect and Have connection
                    waitforConnection();
                    setupStreams();
                    whileChatting();
                } finally {
                    closeCrap();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

I can run that without an exception.

The Problem is your main method

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

Is not in the class where you run it.

//separate file A.java
public class A
{}

//separate file B.java
public class B
{
    public static void main (String[] args) {}

}

If you now run B, it works, because B.java has a main method. BUT: if you run A.java it says: no main methode found. The file you want to run (A, B, etc. must have a main method defined) If you run a java file, it will look for a main method (start point of execution).

0
votes

The main method must be a method of the class:

import java.net.Socket;

public class Server {

    public static void main (String[] args) {
      Server s=new Server();
      s.runningserver();
    }   

So it must come after the class declaration.