tags:

views:

57

answers:

3

Hi,

My Java is very rusty and I'm having a bit of a headache here.

Here's my server.java:

import java.io.IOException;

import net.tootallnate.websocket.WebSocket;
import net.tootallnate.websocket.WebSocketServer;

public class server extends WebSocketServer
{
        public server(int port)
        {
                super(port, Draft.AUTO);
        }

        public void onClientOpen(WebSocket conn)
        {
                System.out.println("onopen");
                /*try
                {
                        this.sendToAll(conn+" entered the room!");
                }
                catch(IOException ex)
                {
                        ex.printStackTrace();
                }*/
        }

        public void onClientClose(WebSocket conn)
        {
                System.out.println("onclose");
        }

        public void onClientMessage(WebSocket conn,String message)
        {
                System.out.println("onmessage"+message);
        }

        public static void main(String[] args)
        {
                int port = 8000;
                try
                {
                        port=Integer.parseInt(args[0]);
                }
                catch(Exception ex)
                {
                }
                server s = new server(port);
                s.start();
                System.out.println("ChatServer started on port: "+s.getPort());
        }
}

I need to include a jar file: ./Java-WebSocket/dist/WebSocket.jar (available from http://github.com/TooTallNate/Java-WebSocket/)

It compiles OK:

prompt> javac -classpath ./Java-WebSocket/dist/WebSocket.jar server.java

However, when I go to run:

prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar server

Exception in thread "main" java.lang.NoClassDefFoundError: server
Caused by: java.lang.ClassNotFoundException: server
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: server.  Program will exit.

I hope someone can help.

Thanks in advance,

+2  A: 
java -classpath .:./Java-WebSocket/dist/WebSocket.jar server
                ^^

Or on Windows:

java -classpath .;./Java-WebSocket/dist/WebSocket.jar server
                ^^

The classpath defaults to the current directory. You need to point to your classes as well as the library.

Tom Hawtin - tackline
Legend, it's working now.
Eamorr
A: 

Isn't server placed into a package ? If it is, specify the full class name.

e.g. prompt> java -classpath ./Java-WebSocket/dist/WebSocket.jar app.core.server

I would also uppercase the class name, though.

thelost
A: 

Have you tried using the full class name (your.package.server)?

JCD