Rmi 错误 IllegalArgumentException、MarshalException [英] Rmi Mistake IllegalArgumentException, MarshalException

查看:55
本文介绍了Rmi 错误 IllegalArgumentException、MarshalException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全班

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

客户端类

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 和所有同步方法并使用服务器" UnicastRemoteObject.ExportObject 我将拥有这个:

If I remove from all class - UnicastRemoteObject and all syncronized metods and use is "Server" UnicastRemoteObject.ExportObject I will have this:

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

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

People) I need your help) What I misundesrstand? What I miss?

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

Thank all for help. I added to IdCl extend Serializible ant it start works.

但我还有一个问题))这是

But I have another problem)) It is

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.capacity 替换为 serverlist.size .... 我的愚蠢错误)

Ok I replaced method serverlist.capacity for serverlist.size.... My stupid mistake)

推荐答案

关于第一个问题,你在没有重新编译或重新部署客户端代码的情况下更改了远程接口,客户端代码应该不再编译,除非你已经更改它,其中如果您还没有重新编译或重新部署它.

Re the first issue, you changed your remote interface without recompiling or redeploying the client code, which should no longer compile, unless you've changed it, in which case you still haven't recompiled or redeployed it.

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

The second case changes the semantics of the application, in that IdCl becomes a non-remote object, which is a completely different kettle of fish. Unclear why you even suggested it.

只需清理、构建、修复和重新部署即可.

Just clean, build, fix, and redeploy.

在下面重新评论,将 Serializable 添加到 ClientInterface 完全没有任何作用,因为 ClientCl 扩展 UnicastRemoteObject 其中 (a) 已经是 Serializable 和 (b) 永远不会被序列化,因为它是通过构造导出的远程对象.你需要按照我在回答中说的去做.当您调用 ClientCl 的方法时,它们会在导出 ClientCl 的主机上打印.否则这不是真正的代码.

Re your comment below, adding Serializable to ClientInterface accomplishes exactly nothing, as ClientCl extends UnicastRemoteObject which (a) is already Serializable and (b) never gets serialized, because it is an exported remote object by construction. You need to do what it says in my answer. When you call methods of ClientCl they print at the host ClientCl was exported from. Or else this is not the real code.

这篇关于Rmi 错误 IllegalArgumentException、MarshalException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆