Java JAR问题

时间:2010-08-11 20:11:50

标签: java jar

我的Java非常生疏,我在这里有点头疼。

这是我的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());
        }
}

我需要包含一个jar文件:./ Java-WebSocket / did / WebSocket.jar(可从http://github.com/TooTallNate/Java-WebSocket/获得)

编译好了:

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

然而,当我去跑步时:

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.

我希望有人可以提供帮助。

提前致谢,

3 个答案:

答案 0 :(得分:2)

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

或在Windows上:

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

类路径默认为当前目录。您需要指向您的课程以及图书馆。

答案 1 :(得分:0)

不是server放入包中吗?如果是,请指定完整的班级名称。

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

但我也会将类名称大写。

答案 2 :(得分:0)

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