【问题标题】:Constantly recieve java.lang.ClassNotFoundException不断收到 java.lang.ClassNotFoundException
【发布时间】:2020-11-26 17:30:59
【问题描述】:

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

我试过了:

  • 使用“package”删除所有行
  • 更改类路径变量
  • 重新安装 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();
        }
    }
}

【问题讨论】:

  • 教程指出您应该通过运行此命令java -classpath classDir example.hello.Server &amp; 来启动服务器,但您似乎省略了example.hello. 部分。
  • 是的,这不起作用,我的讲师指示我删除包的东西,因此删除了 example.hello。片。它在任何一种情况下都不起作用。但是我偶然找到了一个解决方案,现在就发布它。

标签: java rmi classnotfoundexception rmiregistry


【解决方案1】:

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

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

【讨论】:

  • 或者告诉它作为它的类路径。或者使用代码库功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2017-11-03
  • 1970-01-01
  • 2014-12-01
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多