-1

这是代码:

聊天客户端程序:

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

聊天客户端Prog2:

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

聊天服务器程序:

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


    }
}

聊天服务器Prog2:

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


    }
}

直到昨天它都工作正常,但突然之间它开始抛出这些异常 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,我什至将策略文件放在同一个文件夹中。

那么这个问题可能是什么?

聊天界面:

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

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

}

聊天类:

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

1 回答 1

0

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

  1. 使用包
  2. 坚持 Java 命名约定
  3. 使用访问修饰符
  4. 使用 IDE - Netbeans 或 Eclipse
  5. 阅读教程。
于 2013-11-04T14:43:13.550 回答