可以编译但不能运行代码

时间:2010-11-04 16:57:19

标签: java

  

可能重复:
  Classic error, NoClassDefFoundError

之前的问题是我使用javac而不是java的错误,我能够编译但无法运行代码

...> java -cp xSocket-2.8.14.jar SocketClient

产生

import org.xsocket.connection.*;
import java.io.IOException;

public class SocketClient {

    public static void main(String[] args) {    
        try {
            IBlockingConnection bc = new BlockingConnection("127.0.0.1", 8090);
            String req = "Hello server";
            bc.write(req + "\r\n");
        } catch (IOException e) {

        }
        System.out.println("missing");
    }
}

C:\Users\Wildfire\Desktop>java -cp xSocket-2.8.14.jar SocketClient.java
Exception in thread "main" java.lang.NoClassDefFoundError: SocketClient/java
Caused by: java.lang.ClassNotFoundException: SocketClient.java
        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: SocketClient.java.  Program will exit.

2 个答案:

答案 0 :(得分:8)

您运行没有扩展名的类文件,而不是java文件。

java -cp xSocket-2.8.14.jar SocketClient

答案 1 :(得分:-1)

您应指定要执行的类的完全限定名称。所以正确的命令应该是:

java -cp xSocket-2.8.14.jar SocketClient