简体   繁体   English

RMI:ServerException、RemoteException、UnmarshalException、UnmarshalException

[英]RMI: ServerException, RemoteException, UnmarshalException, UnmarshalException

I followed this tutorial: https://docs.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html#5我遵循了本教程: https://docs.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html#5

I compiled them at command prompt and after that I started rmiregistry.我在命令提示符下编译了它们,然后我启动了 rmiregistry。 Then, after I typed:然后,在我输入后:

    start java -classpath "." example.Server

it didn't show "Server ready."它没有显示“服务器准备就绪”。 But, it didn't display any errors as well.但是,它也没有显示任何错误。

While when I tried it at Eclipse, it showed this:当我在 Eclipse 尝试它时,它显示了这一点:

  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: example.Hello
    java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: example.Hello

Hello.java你好。java

   package example;

   import java.rmi.Remote;
   import java.rmi.RemoteException;

   public interface Hello extends Remote
   {
        String sayHello() throws RemoteException;
   }

Server.java服务器.java

    package example;

    import java.rmi.registry.Registry;
    import java.rmi.registry.LocateRegistry;
    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(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();
    }
}

} }

Client.java客户端.java

   package example;

   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();
            }
      }
   }

Are there any steps that I missed?有没有我错过的步骤? What should I do?我应该怎么办?

ClassNotFoundException: example.Hello - your application does not find this class. ClassNotFoundException: example.Hello - 您的应用程序找不到此 class。 Add this class to the class path, both to the client and to the server.将此 class 添加到客户端和服务器的 class 路径。

If you launch your client on some other PC than Sserver, don't forget to copy your class example.Hello together with your client.如果您在 Sserver 以外的其他 PC 上启动客户端,请不要忘记复制您的example.Hello示例。您好与您的客户端一起。

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

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