A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 温昌寿 中级黑马   /  2012-2-22 10:54  /  1828 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 温昌寿 于 2012-2-24 18:04 编辑

Exception in thread "main" java.lang.NoClassDefFoundError: Chatserver (wrong nam
e: ChatServer)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)

8 个回复

倒序浏览
你把你的代码贴出来啊,好多不同的程序都可以报这样的错误.
这样看不出来什么头绪!
回复 使用道具 举报
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
        boolean started = false;
        ServerSocket ss = null;
       
        List<Client> clients = new ArrayList<Client>();
       
        public static void main(String[] args) {

                        System.out.println("aa\n");
                new ChatServer().start();
        }
       
        public void start() {
                try {

                        started = true;
                        ss = new ServerSocket(8888);
                        System.out.println("aa\n");
                } catch (BindException e) {
                        System.out.println("端口使用中....");
                        System.out.println("请关掉相关程序并重新运行服务器!");
                        System.exit(0);
                } catch (IOException e) {
                        e.printStackTrace();
                }
               
                try {
                       
                        while(started) {
                                Socket s = ss.accept();
                                Client c = new Client(s);
System.out.println("a client connected!");
                                new Thread(c).start();
                                clients.add(c);
                                //dis.close();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                ss.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }
       
        class Client implements Runnable {
                private Socket s;
                private DataInputStream dis = null;
                private DataOutputStream dos = null;
                private boolean bConnected = false;
               
                public Client(Socket s) {
                        this.s = s;
                        try {
                                dis = new DataInputStream(s.getInputStream());
                                dos = new DataOutputStream(s.getOutputStream());
                                bConnected = true;
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
               
                public void send(String str) {
                        try {
                                dos.writeUTF(str);
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
               
                public void run() {
                        try {
                                while(bConnected) {
                                        String str = dis.readUTF();
System.out.println(str);
                                        for(int i=0; i<clients.size(); i++) {
                                                Client c = clients.get(i);
                                                c.send(str);
//System.out.println(" a string send !");
                                        }
                                        /*
                                        for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
                                                Client c = it.next();
                                                c.send(str);
                                        }
                                        */
                                        /*
                                        Iterator<Client> it = clients.iterator();
                                        while(it.hasNext()) {
                                                Client c = it.next();
                                                c.send(str);
                                        }
                                        */
                                }
                        } catch (EOFException e) {
                                System.out.println("Client closed!");
                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                try {
                                        if(dis != null) dis.close();
                                        if(dos != null) dos.close();
                                        if(s != null)  {
                                                s.close();
                                                s=null;
                                                //s = null;
                                        }
                                } catch (IOException e1) {
                                        e1.printStackTrace();
                                }
                        }
                }
               
        }
}

回复 使用道具 举报
我是用vim写的,复制到Eclipse就可以运行,但是在dos下用javac就不行
回复 使用道具 举报
NoClassDefFoundError:当 Java 虚拟机或 ClassLoader 实例试图在类的定义中加载(作为通常方法调用的一部分或者作为使用 new 表达式创建的新实例的一部分),但无法找到该类的定义时,抛出此异常。 当前执行的类被编译时,所搜索的类定义存在,但无法再找到该定义。
你给的错误信息是说找不到 Chatserver 的定义。
回复 使用道具 举报
Exception in thread "main" java.lang.NoClassDefFoundError: Chatserver (wrong name: ChatServer) //是大小写没注意吧
回复 使用道具 举报
没有包名吗?如果有包名则要有包名了。也有可能是上楼说的。
回复 使用道具 举报
应该跟包没关系
回复 使用道具 举报
确认下你的当前目录在不在classpath路径下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马