gpt4 book ai didi

java - RMI: java.lang.ClassCastException: com.sun.proxy.$Proxy1 无法转换为 Funktion

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

我正在研究一个正在发挥作用的差异化因素。现在我也希望它是远程的。所以我有一个“函数”接口(interface),一个实现函数接口(interface)的“Funktion”类。然后我有一个服务器、一个客户端和一个通用服务接口(interface)。部分代码:

public class DifferentiatorClient {

public static void main(String[] args) throws RemoteException, MalformedURLException, IllegalArgumentException, NotBoundException, RecognitionException {
execute();
}

public static Double execute() throws RemoteException, MalformedURLException, NotBoundException, IllegalArgumentException, RecognitionException {
Service<Funktion, Double, Double> service;
String url = "//localhost/DifferentiatorService";
Double r;
Script script = new Script();
Funktion f = script.getFunction("f");
service = (Service<Funktion, Double, Double>) Naming.lookup(url);
r = service.execute(f, 2.0);
return r;

}

}

“Funktion”类:

public class Funktion extends UnicastRemoteObject implements Function, Serializable {

/**
*
*/
private static final long serialVersionUID = -1234132759512350836L;
//some methods
}

“函数”界面:

public interface Function  extends java.rmi.Remote {

double eval(final double ... args) throws IllegalArgumentException, RecognitionException, RemoteException;
}

服务器:

public class DifferentiatorService extends java.rmi.server.UnicastRemoteObject
implements Service<Funktion, Double, Double> {

private static final long serialVersionUID = -3236697150408344006L;

protected DifferentiatorService() throws RemoteException {

}

@Override
public String getName() throws RemoteException {
return "DifferentiatorService";

}

public Double execute(Funktion f, Double... args) throws RemoteException {
Differentiator diff = new Differentiator();
double result = diff.differentiate(f, args[0]);
return result;
}

public static void main(String[] args) throws Exception {
String url = "//localhost/DifferentiatorService";
Registry registry = LocateRegistry.createRegistry(1099);
DifferentiatorService service = new DifferentiatorService();
Naming.rebind(url, service);
}

}

最后是服务接口(interface):

public interface Service<T,A,R> extends java.rmi.Remote {

String getName() throws java.rmi.RemoteException;

R execute(T task,A ... args) throws java.rmi.RemoteException;
}

对这堵代码感到抱歉,我只是认为可能有必要找到错误。首先,如果我使用如下服务:

Service<String, Double, Double> service;然后在“服务器端”解析字符串并返回结果,其工作完美。 Actor 阵容:service = (Service<String, Double, Double>) Naming.lookup(url);那么就不会产生任何问题。

但是我试图让它与 Funktion 对象一起工作。我总是从标题中得到异常(exception)。我在谷歌上搜索了很多,人们说转换到接口(interface)而不是具体的类很重要。我想我正在转换到一个界面

service = (Service<Funktion, Double, Double>) Naming.lookup(url);

我也尝试过:

service = (Service<Function, Double, Double>) Naming.lookup(url);

但遇到同样的异常。

我希望有人能帮助我。最好的问候

编辑:在客户端和服务器中使用接口(interface)“Function”会导致此异常:

Exception in thread "main" java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: 
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:229)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy1.execute(Unknown Source)
at de.lab4inf.wrb.DifferentiatorClient.execute(DifferentiatorClient.java:25)
at de.lab4inf.wrb.DifferentiatorClient.main(DifferentiatorClient.java:13)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:215)
... 6 more

最佳答案

您无法将 stub 强制转换为实现类Funktion。它不是该类的实例。它是远程接口(interface)的一个实例。因此,将其转换为远程接口(interface):Function。并相应地更改整个 Service 类的签名。

注意,您是在自找麻烦,让这些名称保持如此相似。将第一个更改为 FunctionImplFunctionServerFunctionRemoteObject 或其他。

关于java - RMI: java.lang.ClassCastException: com.sun.proxy.$Proxy1 无法转换为 Funktion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27885751/

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