简体   繁体   English

在加载程序之间进行转换时如何修复 ClassCastException? Java RMI

[英]How to fix ClassCastException when casting between loaders? Java RMI

Very new to java rmi. java rmi 非常新。 I am trying to create a stub of my server object with the lines:我正在尝试使用以下行创建我的服务器 object 的存根:

SorterServer obj = new SorterServer();
Sorter stub = (Sorter) UnicastRemoteObject.exportObject((Remote) obj, 0);

however, casting 'obj' to Remote throws the error但是,将“obj”转换为 Remote 会引发错误

java.lang.ClassCastException: class SorterServer cannot be cast to class java.rmi.Remote (SorterServer is in unnamed module of loader 'app'; java.rmi.Remote is in module java.rmi of loader 'bootstrap')

UnicastRemoteObject is the only way I know of creating a stub, and it requires the object to be cast to a Remote. UnicastRemoteObject 是我知道的创建存根的唯一方法,它需要将 object 强制转换为 Remote。 This exact line of code worked in a tutorial I was following, so I am not sure why it produces this error here.这行代码在我正在关注的教程中工作,所以我不确定它为什么会在这里产生这个错误。 I have done some research on this error, and loaders, but didn't really find anything specific to me.我已经对此错误和加载程序进行了一些研究,但并没有真正找到任何特定于我的东西。 Sorry if this is extremely obvious, as I have not done this stuff before.抱歉,如果这非常明显,因为我以前没有做过这些事情。

Is SorterServer your class? SorterServer 是您的 class 吗? Does it implement Remote?它是否实现远程?

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

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