首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RMI: ServerException、RemoteException、UnmarshalException、UnmarshalException

RMI: ServerException、RemoteException、UnmarshalException、UnmarshalException
EN

Stack Overflow用户
提问于 2019-10-02 04:25:12
回答 1查看 326关注 0票数 2

我遵循了这个教程:https://docs.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html#5

我在命令提示符下对它们进行了编译,然后启动了rmiregistry。然后,在我输入之后:

代码语言:javascript
复制
    start java -classpath "." example.Server

它没有显示“服务器就绪”。但是,它也没有显示任何错误。

而当我在Eclipse上尝试它时,它显示了:

代码语言:javascript
复制
  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

代码语言:javascript
复制
   package example;

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

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

Server.java

代码语言:javascript
复制
    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

代码语言:javascript
复制
   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();
            }
      }
   }

我有没有遗漏什么步骤?我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2019-10-02 04:58:13

ClassNotFoundException: example.Hello -您的应用程序找不到此类。将此类添加到类路径中,包括客户端和服务器。

如果您在服务器之外的其他PC上启动客户端,不要忘记将您的类example.Hello与客户端一起复制。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58191796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档