java - Java RMI:未找到远程接口,抛出 java.lang.ClassNotFoundException,仅在全新启动后有效
问题描述
在 Java RMI 程序期间,似乎缺少远程接口 (CalcInterface.java)。
所有的 java 文件都在项目的 src 文件夹中(我使用的是 IDEA)。没有 gradle,什么都没有。
有:
- CalcClass.java
- CalcInterface.java
- 客户端.java
- 服务器.java
他们都编译了项目的out/production/RMI
目录。
RMI 是项目的名称。
IDEA 没有给出编译错误。
但是,在运行 Server.main()
时出现以下异常:
Server Exception:java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalcInterface
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalcInterface
...
它在新启动时工作,但在我 ctrl+C 服务器程序并重新运行它之后。它再也不会起作用了。是的,我正在释放端口并重新启动 rmiregistry。
我不明白为什么它不能访问 CalcInterface。
我还尝试通过在 src 文件夹中使用 javac 然后运行来编译所有这些 java 文件,java Server
但这会产生相同的错误。
在这里转储四个java文件因为我不知道如何指定问题。
CalcInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface CalcInterface extends Remote {
int add(int a, int b) throws RemoteException;
int subtract(int a, int b) throws RemoteException;
int multiply(int a, int b) throws RemoteException;
double divide(int a,int b) throws RemoteException, ArithmeticException;
}
CalcClass.java
import java.rmi.RemoteException;
public class CalcClass implements CalcInterface{
@Override
public int add(int a, int b) throws RemoteException {
return a+b;
}
@Override
public int subtract(int a, int b) throws RemoteException {
return a-b;
}
@Override
public int multiply(int a, int b) throws RemoteException {
return a*b;
}
@Override
public double divide(int a, int b) throws RemoteException, ArithmeticException {
return (double)a/b;
}
}
Server.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server {
public static void main(String[] args) {
try {
// Instantiating implementation class
CalcClass obj = new CalcClass();
// Exporting object of implementation class to stub
CalcInterface stub = (CalcInterface) UnicastRemoteObject.exportObject(obj,5000);
// Binding stub to registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Calculator", stub);
System.out.println("Server Ready");
}
catch (Exception e){
System.err.println("Server Exception:" + e.toString());
e.printStackTrace();
}
}
}
没有附上Client.java
,因为我认为它与这个问题没有任何关系。
非常感谢有关该问题或一般有关 RMI 或 Java 的任何建议。
谢谢你!
解决方案
使用java.rmi.server.codebase
财产。这充当源,从中将类加载到虚拟机中。
你可以从这里参考它。
推荐阅读
- windows - 无法通过 Jenkins 执行 bat 文件
- python-3.x - 如何使用 ffmpeg 将音频切割成重叠的片段?
- java - 将 maven pom.xml 从 java 8 升级到 13。如何在安装了 java 8 和 13 的机器上运行升级的 maven 项目
- javascript - 单击按钮后,消息出现在同一个 div 中
- solr - Windows 10 上的 solr 入门 - 我的第一个本地(非云)集合/核心
- java - 如何将整数传递给参数类型是整数数组而不是整数的实例
- java - 为扩展为级别的方法授予不同的权限
- javascript - 如何切换主题以响应动态导入
- python - 如何在 Python 中将前景文本与嘈杂的背景分开?
- android - 使用 Jenkins 构建 Android 项目时出现问题(未找到任务)