简体   繁体   中英

Java JAR problem

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,

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.

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

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

I would also uppercase the class name, though.

您是否尝试过使用完整的类名(your.package.server)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM