2013-11-04 102 views
-1

這個Java RMI的例外是代碼:在服務器和客戶端

chatClientProg:

import java.rmi.*; 
import java.rmi.registry.*; 
import java.util.Scanner; 
import java.io.*; 

class chatClientProg{ 
    public static void main(String args[]){ 
     Scanner input = new Scanner(System.in); 
     String myMessage,userName,infoString; 
     InputStreamReader convertor = new InputStreamReader(System.in); 
     BufferedReader in = new BufferedReader(convertor); 
     System.out.println("Enter your name :"); 
     userName = input.nextLine(); 

     try{ 
     chatInterface objHandle = (chatInterface)Naming.lookup("rmi://192.168.42.58/sumitChatApp/myChatRemoteObj"); 
     infoString = userName + " joined the chat."; 
     chatCustomMessage infoMessage = new chatCustomMessage(infoString); 
     infoMessage = objHandle.printMessage(infoMessage); 

     //Runtime.getRuntime().exec("cmd /c cls");  // not working on the current cmd... 
     System.out.println("Welcome to the Chat Application "+userName+"!"); 
     Thread.sleep(1500); 
     //Runtime.getRuntime().exec("cmd /c cls"); 

     while(true){ 
      myMessage = in.readLine(); 
      myMessage = userName+" : "+myMessage; 
      chatCustomMessage mess = new chatCustomMessage(myMessage); 
      mess = objHandle.printMessage(mess); 
     } 
     } 
     catch(Exception ex){ 
      System.out.println("Exception caught"); 
      ex.printStackTrace(); 
     } 
    } 
} 

chatClientProg2:

// this is the client program 

import java.rmi.*; 
import java.rmi.registry.*; 
import java.util.Scanner; 
import java.io.*; 

class chatClientProg2{ 
    public static void main(String args[]){ 
     Scanner input = new Scanner(System.in); 
     String myMessage,userName,infoString; 
     InputStreamReader convertor = new InputStreamReader(System.in); 
     BufferedReader in = new BufferedReader(convertor); 
     System.out.println("Enter your name :"); 
     userName = input.nextLine(); 


     try{ 
     chatInterface objHandle = (chatInterface)Naming.lookup("rmi://192.168.42.58/sumitChatApp/myChatRemoteObj"); 
     infoString = userName + " joined the chat."; 
     chatCustomMessage infoMessage = new chatCustomMessage(infoString); 
     infoMessage = objHandle.printMessage(infoMessage); 

     //Runtime.getRuntime().exec("cmd /c cls");  // not working on the current cmd... 
     System.out.println("Welcome to the Chat Application "+userName+"!"); 
     Thread.sleep(1500); 
     //Runtime.getRuntime().exec("cmd /c cls"); 

     while(true){ 
      myMessage = in.readLine(); 
      myMessage = userName+" : "+myMessage; 
      chatCustomMessage mess = new chatCustomMessage(myMessage); 
      mess = objHandle.printMessage(mess); 
     } 
     } 
     catch(Exception ex){ 
      System.out.println("Exception caught"); 
      ex.printStackTrace(); 
     } 
    } 
} 

chatServerProg:

// server program... i.e chatServerProg 

import java.rmi.*; 
import java.rmi.server.*; 
import java.rmi.registry.*; 

class chatServerProg{ 
    public static void main(String args[]){ 
     try{ 
     /* Set the security manager */ 
     System.setProperty("java.security.policy","policy.all"); 
     System.setSecurityManager(new RMISecurityManager()); 

     chatClass obj = new chatClass(); 
     Naming.rebind("rmi://192.168.42.58/sumitChatApp/myChatRemoteObj",obj); 
     } 
     catch(Exception ex){ 
      System.out.println("Exception caught"); 
      ex.printStackTrace(); 
     } 


    } 
} 

chatServerProg2:

// server program... i.e chatServerProg 

import java.rmi.*; 
import java.rmi.server.*; 
import java.rmi.registry.*; 

class chatServerProg2{ 
    public static void main(String args[]){ 
     try{ 
     /* Set the security manager */ 
     System.setProperty("java.security.policy","policy.all"); 
     System.setSecurityManager(new RMISecurityManager()); 

     chatClass obj = new chatClass(); 
     Naming.rebind("rmi://192.168.42.58/sumitChatApp/myChatRemoteObj",obj); 
     } 
     catch(Exception ex){ 
      System.out.println("Exception caught"); 
      ex.printStackTrace(); 
     } 


    } 
} 

它一直工作得很好,直到昨天,但突然它開始拋出這些exciption idk爲什麼。

客戶端的異常:

Enter your name : 
sid 
Exception caught 
java.rmi.NotBoundException: sumitChatApp/myChatRemoteObj 
     at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:106) 
     at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source) 
     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386 
) 
     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250) 
     at sun.rmi.transport.Transport$1.run(Transport.java:159) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at sun.rmi.transport.Transport.serviceCall(Transport.java:155) 
     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5 
35) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor 
t.java:790) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport 
.java:649) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec 
utor.java:885) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor 
.java:907) 
     at java.lang.Thread.run(Thread.java:619) 
     at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknow 
n Source) 
     at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source) 
     at sun.rmi.server.UnicastRef.invoke(Unknown Source) 
     at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) 
     at java.rmi.Naming.lookup(Unknown Source) 
     at chatClientProg.main(chatClientProg.java:18) 

服務器端異常:

Exception caught 
java.rmi.ServerException: RemoteException occurred in server thread; nested exce 
ption is: 
     java.rmi.UnmarshalException: error unmarshalling arguments; nested excep 
tion is: 
     java.lang.ClassNotFoundException: chatInterface 
     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396 
) 
     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250) 
     at sun.rmi.transport.Transport$1.run(Transport.java:159) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at sun.rmi.transport.Transport.serviceCall(Transport.java:155) 
     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5 
35) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor 
t.java:790) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport 
.java:649) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec 
utor.java:885) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor 
.java:907) 
     at java.lang.Thread.run(Thread.java:619) 
     at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknow 
n Source) 
     at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source) 
     at sun.rmi.server.UnicastRef.invoke(Unknown Source) 
     at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) 
     at java.rmi.Naming.rebind(Unknown Source) 
     at chatServerProg.main(chatServerProg.java:15) 
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex 
ception is: 
     java.lang.ClassNotFoundException: chatInterface 
     at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source) 
     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386 
) 
     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250) 
     at sun.rmi.transport.Transport$1.run(Transport.java:159) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at sun.rmi.transport.Transport.serviceCall(Transport.java:155) 
     at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5 
35) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor 
t.java:790) 
     at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport 
.java:649) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec 
utor.java:885) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor 
.java:907) 
     at java.lang.Thread.run(Thread.java:619) 
Caused by: java.lang.ClassNotFoundException: chatInterface 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:200) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:188) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:251) 
     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) 
     at java.lang.Class.forName0(Native Method) 
     at java.lang.Class.forName(Class.java:247) 
     at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:7 
11) 
     at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655) 
     at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592) 
     at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:6 
28) 
     at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294 
) 
     at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStrea 
m.java:238) 
     at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1531) 
     at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493) 
     at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1 
732) 
     at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) 
     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) 
     ... 12 more 

我現在已經在所有的四個方案正確更改IP地址,我運行rmiregistry中每次,我甚至有策略文件放在同一個文件夾中。

那麼這可能是什麼問題?

chatInterface:

// this is the interface for the sumitChat 
import java.rmi.*; 

interface chatInterface extends Remote{ 
    public chatCustomMessage printMessage(chatCustomMessage arg) throws RemoteException; 

} 

chatClass:

// the chatClass that will implement the chatInterface 
import java.rmi.*; 
import java.rmi.server.*; 
import java.rmi.registry.*; 

class chatClass extends UnicastRemoteObject implements chatInterface{ 
    public chatClass() throws RemoteException{ 
     // constructor 
    } 
    public chatCustomMessage printMessage(chatCustomMessage obj) throws RemoteException{ 
     System.out.println(obj.message); 
     return obj; 
    } 
} 
+0

當沒有任何名稱綁定遠程對象時,可以重新綁定遠程對象嗎?除此之外,這不是你的問題。 * java.lang.ClassNotFoundException:chatInterface *'chatInterface'不在類路徑上 – Antoniossss

+0

不是。下面是代碼 –

+0

不,不是,那是異常說的,我猜JVM知道你在做什麼。 – Antoniossss

回答

0

chatClass應根據應用程序的邏輯實現你的界面和Serializable。它不應該延伸UnicastRemoteObject。 至於未來的

  1. 使用包
  2. 堅持到Java命名約定
  3. 使用訪問修飾符
  4. 使用IDE - Netbeans的或者Eclipse
  5. this教程。
+0

非常感謝你的朋友:) –

+0

絕對不是。如果聊天類沒有擴展UnicastRemoteObject,它就不再是一個遠程對象,交互也不再是RMI:它成爲一個移動代理系統。我不知道爲什麼你在標記爲RMI的問題中提出這個建議。 -1 – EJP