gpt4 book ai didi

java - Rmi 错误 IllegalArgumentException、MarshalException

转载 作者:行者123 更新时间:2023-12-02 02:16:01 25 4
gpt4 key购买 nike

全民类(class)

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

客户端接口(interface)

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

服务器接口(interface)

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 我将得到以下内容:

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

最佳答案

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

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

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

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

关于java - Rmi 错误 IllegalArgumentException、MarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49294521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com