server wont run?

i need a little help, me and a friend are writing a Runescape private server, the base is almost done but for some reason the server just wont run.

im getting a run error cant find main.

code:



package lf;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import lf.item.Item;
import lf.util.HostThrottle;

public class Server implements Runnable {

    public Server() {
    }
    public static final int cycleTime = 600;

    public static void main(String args[]) throws Throwable {
        if (args.length != 3) {
            System.out.println("Usage: [OnSource_password] [OnSource_port] [startup_wait]");
            return;
        }
        OnSource.init(args[0], args[1], args[2]);

        Thread t = new Thread() {

            @Override
            public void run() {

                Scanner in = new Scanner(System.in);
                while (true) {
                    try {
                        String[] args = in.nextLine().split(" ");
                        args[0] = args[0].toLowerCase();

                        if (args[0].startsWith("stop")) {
                            System.out.println("Stopping server...");
                            playerHandler.update(Integer.parseInt(args[1]));
                            Thread.sleep(Integer.parseInt(args[1]) * 1000);
                            Server.stopServer = true;
                            Server.join(250L);
                            System.out.println("Server stopped.");
                        }

                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            }
        };
        t.setDaemon(true);
        t.start();

        try {
            Item.initDefs();

            clientHandler = new Server();
            (new Thread(clientHandler)).start();

            playerHandler = new PlayerHandler();

            while (!stopServer) {
                long start = System.nanoTime();
                try {
                    playerHandler.process();

                } finally {
                    long time = 600L - ((System.nanoTime() - start) / 1000000L);
                    if (time > 0L) {
                        Thread.sleep(time);
                    }
                }
            }

        } finally {
            playerHandler.destruct();
            clientHandler.destruct();
            down = true;
        }
    }
    public static boolean down;
    public static Server clientHandler;
    public static ServerSocket clientListener;
    public static boolean stopServer = false;
    public static boolean stopListener;
    public static final int GAME_PORT = 43593;
    public static PlayerHandler playerHandler;

    public static void join(long step) throws InterruptedException {
        while (!Server.down) {
            Thread.sleep(step);
        }
    }

    public void run() {
        HostThrottle ht = new HostThrottle(3);
        ServerSocket ss = null;
        try {
            try {
                ss = new ServerSocket(GAME_PORT);
                Misc.println("Game server listening on " + ss.getInetAddress().getHostAddress() + ":" + ss.getLocalPort());

                do {
                    Socket sock = ss.accept();
                    if (ht.register(sock)) {
                        sock.setTcpNoDelay(true);
                        sock.setTrafficClass(0x04 | 0x08);
                        playerHandler.newPlayer(sock);

                    } else {
                        sock.close();
                    }

                } while (!stopListener);

            } finally {
                if (ss != null) {
                    ss.close();
                }
            }

        } catch (IOException e) {
            if (!stopListener) {
                e.printStackTrace();
            } else {
                Misc.println("Game listener shut down");
            }
        }
    }

    public void destruct() {
        try {
            stopListener = true;
            if (clientListener != null) {
                clientListener.close();
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

Do you have your files in a jar? if so you need a Manifest.mf file in the meta-inf fold. The manifast file have a line with Main-Class: lf.Server

its not built into a jar, im just running it from the source.

How exactly do you run it? From within an IDE or from the command line?

eclipse ide

It doesn’t have a regular main method. The normal one doesn’t throw anything, because it’s the first entrypoint. Yours throws throwable.

If you open the run configuration (click on the little arrow next to the run button and choose “Run Configurations” and then select the entry you are running) in Eclipse, is the “Main Class” section showing lf.Server?

Mike

It doesn’t matter, the main method can throw whatever it wants.

You shouldn’t be throwing any exceptions in the #main() method. Nothing calls it.

It would be best to catch the exception in the main method so you can log it for debugging.

@Conner__ The ClassLoader calls it… herp derp

Whilst it’s bad programming practices (Not only for debugging purposes) to throw an exception from the main method, it is allowed.

This however, isn’t his problem. His error isn’t a problem with his code, it’s his IDE. This is a common error with eclipse. Because Eclipse allows you to run different projects, it requires you to manually select the project to be run. In some cases the project will have more than one main method, in which case you would need to go through the options in eclipse and select the main class that is the “real” main class.

If the problem is fixed then just back up your code and create another project… It will usually just be a bug with the project.

Usually hitting the “Run” button while focused on the class that has the main method or is associated with another class that has the main method works fine for me.

A GUI app should probably catch every exception and log it or present it to the user somehow. But if you know you’ll have a console up anyway (which you will have in your IDE), there’s nothing wrong with letting exceptions propagate out of main, since the default behavior of printing the stack trace is probably what you want anyway.

Chalk up another reason why I like IDEA: Shift-Ctrl-F10 figures out what the proper run configuration is for the current window, and only ever runs that, not something you set up days or months ago. Hitting it where there’s a main method runs the app, hitting it in a test runs the test, hitting it in some random text file does nothing at all.