【发布时间】:2020-11-26 17:30:59
【问题描述】:
我对 Java 和 RMI 系统非常陌生。我正在学习教程,但我不确定为什么我不断收到以下错误[1] [1]:https://i.stack.imgur.com/xeYTn.png 我附上了代码(直接取自这里的教程:https://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html)
我试过了:
- 使用“package”删除所有行
- 更改类路径变量
- 重新安装 java 和 javac
- 在“rmiregistry &”命令中设置类路径
任何帮助将不胜感激
编辑:哎呀,忘了附上代码。 你好.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
客户端.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
服务器.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject((Remote) obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
【问题讨论】:
-
教程指出您应该通过运行此命令
java -classpath classDir example.hello.Server &来启动服务器,但您似乎省略了example.hello.部分。 -
是的,这不起作用,我的讲师指示我删除包的东西,因此删除了 example.hello。片。它在任何一种情况下都不起作用。但是我偶然找到了一个解决方案,现在就发布它。
标签: java rmi classnotfoundexception rmiregistry