Rmi Mistake IllegalArgumentException,MarshalException

时间:2018-03-15 08:23:57

标签: java rmi

所有课程

package Task2;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class IdCl extends UnicastRemoteObject {
    private int id;
    private String name;

    protected IdCl() throws RemoteException {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }
}

客户端界面

package Task2;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ClientInt extends Remote {
    public void printMe(String s) throws RemoteException;
    public void onePrint(String s) throws RemoteException;
}

CLient类

package Task2;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ClientCl extends UnicastRemoteObject implements ClientInt {


    protected ClientCl() throws RemoteException {
    }

    public void printMe(String s) throws RemoteException {
        System.out.println(s);
    }
    public void onePrint(String s) throws RemoteException {
        System.out.println(s);
    }
}

客户端

package Task2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client  {
    protected Client() throws RemoteException {
    }

    public synchronized static void main(String[] args) throws IOException {
        ServInt servInt = null;
        if (System.getSecurityManager()==null){
            System.setSecurityManager(new SecurityManager());
        }
        try {
            Registry registry = LocateRegistry.getRegistry("localhost",Registry.REGISTRY_PORT);
            servInt= (ServInt) Naming.lookup("server");
        }catch (Exception e){
            System.err.println(e);
            e.printStackTrace();
        }
        IdCl idCl = new IdCl();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        idCl.setName(br.readLine());
         ClientInt clientInt = new ClientCl();

//         servInt.someNew(clientInt);
         servInt.connect(idCl, clientInt);
    }
}

服务器接口

package Task2;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ServInt extends Remote {
    public void connect(IdCl idCl, ClientInt clientInt)throws RemoteException;
    public void someNew(ClientInt clientInt)throws RemoteException;
}

服务器类

package Task2;

import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

public class ServCl extends UnicastRemoteObject implements ServInt {
    protected int bw = 100000;
    private Vector<ClientInt> serverlist;

    protected ServCl() throws RemoteException {
    }

    public void someNew (ClientInt clientInt) throws RemoteException {
        serverlist.addElement(clientInt);
    }

    @Override
    public void connect(IdCl idCl, ClientInt clientInt)  throws RemoteException {
        if (!serverlist.contains(clientInt)) {
            serverlist.addElement(clientInt);
            idCl.setId((int)Math.random()*bw);
            print(clientInt);

        }
            else {
            clientInt.onePrint("Hello from the server "+clientInt.getClass().getName());
            }

        }

        public  void print(ClientInt clientInt) throws RemoteException {
            for (int i = 0; i < serverlist.capacity(); i++) {
                if (!(clientInt.equals(serverlist.get(i)))) {
                    clientInt.printMe("Hello from " + serverlist.get(i).getClass().getName()+". Nice to meet you "+clientInt.getClass().getName());
                }

            }
    }
    }

服务器

package Task2;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Server {
    public static void main(String[] args) throws InterruptedException {
        if (System.getSecurityManager()==null){
            System.setSecurityManager(new SecurityManager());
        }
        try {
            Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
            ServInt servInt = new ServCl();
            registry.rebind("server",servInt);

        }catch (Exception e){
            System.err.println(e);
            e.printStackTrace();
        }
        Thread.sleep(5000);
    }
}

错误1

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:283)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:260)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
    at com.sun.proxy.$Proxy0.connect(Unknown Source)
    at Task2.Client.main(Client.java:34)

如果我从所有类中删除 - UnicastRemoteObject和所有同步的metods并且使用的是“Server”UnicastRemoteObject.ExportObject我将拥有:

Exception in thread "main" java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: Task2.IdCl
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:157)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
    at com.sun.proxy.$Proxy0.connect(Unknown Source)
    at Task2.Client.main(Client.java:34)
Caused by: java.io.NotSerializableException: Task2.IdCl
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:290)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:152)
    ... 4 more

人们)我需要你的帮助)我误解了什么?我错过了什么?

感谢大家的帮助。我添加到IdCl扩展Serializible ant它开始工作。

但我有另一个问题)) 它是

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.get(Vector.java:748)
at Task2.ServCl.print(ServCl.java:35)
at Task2.ServCl.connect(ServCl.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:283)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:260)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
at com.sun.proxy.$Proxy0.connect(Unknown Source)
at Task2.Client.main(Client.java:34)

好的我替换了serverlist.size的方法serverlist.capacity ....我的愚蠢错误)

1 个答案:

答案 0 :(得分:2)

在第一个问题上,您更改了远程接口而没有重新编译或重新部署客户端代码,这些代码不应再编译,除非您已更改它,在这种情况下您仍然无法重新编译或重新部署它

第二种情况改变了应用程序的语义,因为IdCl变成了一个非远程对象,这是一个完全不同的鱼群。不清楚为什么你甚至建议它。

只需清理,构建,修复和重新部署。

在下方重新发表您的评论,将Serializable添加到ClientInterface完全没有任务,因为ClientCl extends UnicastRemoteObject其中(a)已经Serializable而且(b)从未被序列化,因为它是一个通过构造导出的远程对象。你需要在我的回答中做到它所说的。当您调用ClientCl的方法时,他们会在导出的主机ClientCl上打印。否则这不是真正的代码。