gpt4 book ai didi

java - RMI:ServerException、RemoteException、UnmarshalException、UnmarshalException

转载 作者:行者123 更新时间:2023-12-01 06:01:14 27 4
gpt4 key购买 nike

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

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

    start java -classpath "." example.Server

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

当我在 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

   package example;

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

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

服务器.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();
}
}

}

客户端.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();
}
}
}

我是否遗漏了任何步骤?我该怎么办?

最佳答案

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

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

关于java - RMI:ServerException、RemoteException、UnmarshalException、UnmarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com