2010-07-29 76 views
0

我有一个客户端发送一个类对象到服务器。服务器应该调用该类的方法并返回结果。 当我运行我的程序时,出现以下异常java.lang.ClassNotFoundException:newclient.TestObject。java服务器没有从客户端接收对象

server.java:

package newserve; 

import java.net.*; 
import java.io.*; 
import java.lang.reflect.*; 

public class SERVER { 
    public static void main(String[] args) { 
    int port = 9876; 
     try { 
      ServerSocket ss = new ServerSocket(port); 
     Socket s = ss.accept(); 

      InputStream is = s.getInputStream(); 
     ObjectInputStream ois = new ObjectInputStream(is); 

     //read the first object from the socket 
     Object o1 = /*(Object)*/ois.readObject(); 

      //Handling the first received object 
     if (o1 != null){ 
     System.out.println("\nFROM SERVER - receiving class: " + 
             o1.getClass().getName()); 
    System.out.println("\nWith these methods: \n"); 

    //get all the methods into an array 
    Method[] methods = o1.getClass().getDeclaredMethods(); 

    //print the methods 
    for(int i = 0; i < methods.length; i++) 
     System.out.println(methods[i]); 

    //invoking the first method with default constructor 
    Object a = methods[0].invoke(o1.getClass().newInstance(), 
              new Object[] {3, 5}); 

    System.out.println("\nOutput of the first method: " + a); 
    } 

    //read the second object from the socket 
    Object o2 = ois.readObject(); 
    System.out.println("\n\nFROM SERVER - receiving class: " + 
             o2.getClass().getName()); 
    System.out.println("\nWith these: " + o2); 

    //close everything and shut down 
    is.close(); //close input stream 
    s.close(); //close the socket 
    ss.close(); //close the server's socket 

}catch(Exception e){System.out.println(e);} 
} 

}

client.java:

package newclient; 

import java.net.*; 
import java.io.*; 

public class CLIENT { 
    public static void main(String[] args) { 
     int port = 9876; 
    try{ 
     Socket s = new Socket("localhost", port); 
     OutputStream os = s.getOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(os); 

     Object to = new TestObject(); //create a new object 

     oos.writeObject(to); //send the object to the server 

     // create a new String object and send 
     oos.writeObject(new String("A String object from client")); 

     //close the connection 
     oos.close(); 
     os.close(); 
     s.close(); 
    }catch(Exception e){System.out.println(e);} 
    } 
} 

TestObject.java:

package newclient; 

import java.io.*; 

/** 
* A test object to send via socket 
*/ 
class TestObject implements Serializable { 
    static final long serialVersionUID = 0; 
    //constructor 
    public TestObject(){};//default constructor 

    //method 
    public int add(int a, int b){return a+b;} 
    public int sub(int a, int b){ return a-b;} 
} 

谢谢!

+0

对null的测试是毫无意义的,除非您打算编写空值。 – EJP 2010-07-30 10:44:14

回答

2

您的服务器在其类路径中需要newclient.TestObject.class。

你的目录结构应该是这样的:

 
(CWD) 
├── newclient 
│   ├── CLIENT.class 
│   └── TestObject.class 
└── newserver 
    └── SERVER.class 

哪里CWD是当前工作目录。你应该站在那个顶级目录并运行

java -classpath . newserver.SERVER 
+0

如何将newclient.TestObject.class添加到其类路径中? – Atiya 2010-07-30 00:17:48

0

我假设客户端和服务器程序运行在不同的机器上。然后作为nos说你需要添加到服务器的类路径。

您的客户端应该是这样的

(CWD) 
├── newclient 
│ ├── CLIENT.class 
│ └── TestObject.class 

,你应该象这样开始

java -classpath . newclient.CLIENT.class 

您的服务器应该像这样(请注意,它需要TestObject.class)

(CWD) 
├── newclient 
│ └── TestObject.class 
└── newserver 
    └── SERVER.class 

你应该像这样开始

java -classpath。 newserver.CLIENT.class

相关问题