服务器端和客户端的Java RMI异常

时间:2013-11-04 13:25:03

标签: java rmi unmarshalling

这是代码:

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();
        }


    }
}

直到昨天才工作正常,但突然之间就开始抛出这些激动人心的原因了。

客户端例外:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的chatClass应根据应用逻辑实现您的界面和Serializable。它不应该扩展UnicastRemoteObject。 至于未来

  1. 使用套件
  2. 坚持Java命名约定
  3. 使用访问修饰符
  4. 使用IDE - Netbeans或Eclipse
  5. 阅读this教程。