0

我对 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 )

我努力了:

  • 删除任何带有“包”的行
  • 更改类路径变量
  • 重新安装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();
        }
    }
}
4

1 回答 1

0

您必须在代码编译到的文件夹中运行命令 rmiregistry &。在这种情况下,“文件”

“入门”教程没有提到这一点。

于 2020-08-06T13:01:16.463 回答