2010-07-29 2 views
0

클래스 객체를 서버에 보내는 클라이언트가 있습니다. 서버는 해당 클래스의 메소드를 호출하고 결과를 리턴해야합니다. 프로그램을 실행할 때 java.lang.ClassNotFoundException : newclient.TestObject 예외가 발생합니다.자바 서버가 클라이언트로부터 객체를받지 못합니다.

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 :

고맙습니다!

+0

널 (null)에 대한 테스트는 널 (null) 작성을 계획하지 않는 한 무의미합니다. – EJP

답변

2

서버는 클래스 경로에 newclient.TestObject.class가 필요합니다.

디렉토리 구조는 다음과 같이해야한다 : CWD는 현재 작업 디렉토리입니다

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

. 이 최상위 디렉토리에 서서 실행해야합니다.

java -classpath . newserver.SERVER 
+0

newclient.TestObject.class를 classpath에 어떻게 추가합니까? – Atiya

0

클라이언트와 서버 프로그램이 다른 시스템에서 실행된다고 가정합니다. 그런 다음 서버의 클래스 경로에 추가해야한다고 nos가 말했습니다.

당신의 클라이언트가이

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

과 같아야하고 (이 TestObject.class 필요 주)

(CWD) 
├── newclient 
│ └── TestObject.class 
└── newserver 
    └── SERVER.class 
다음과 같아야이

java -classpath . newclient.CLIENT.class 

서버처럼 시작해야

과 같이 시작해야합니다.

java -classpath. newserver.CLIENT.class

관련 문제