我遵循了这个教程: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.HelloHello.java
package example;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws RemoteException;
}Server.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
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();
}
}
}我有没有遗漏什么步骤?我该怎么办?
发布于 2019-10-02 04:58:13
ClassNotFoundException: example.Hello -您的应用程序找不到此类。将此类添加到类路径中,包括客户端和服务器。
如果您在服务器之外的其他PC上启动客户端,不要忘记将您的类example.Hello与客户端一起复制。
https://stackoverflow.com/questions/58191796
复制相似问题