繁体   English   中英

Rmi 错误 IllegalArgumentException, MarshalException

[英]Rmi Mistake 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);
    }
}

错误一

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 我将拥有这个:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM