客户端查找到服务器时获取notboundexception

6jygbczu  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(154)

我试图在oracles的网站上运行rmi教程。我可以运行服务器,但运行客户端时收到错误。尝试运行客户端时收到的错误是notboundexception异常。如何修复此错误?
下面是代码和异常
例外

Exception in thread "main" java.rmi.NotBoundException: Server
    at java.rmi/sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:234)
    at java.rmi/sun.rmi.registry.RegistryImpl_Skel.dispatch(RegistryImpl_Skel.java:133)
    at java.rmi/sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:468)
    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:298)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:691)
    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:832)
    at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
    at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
    at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:380)
    at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:123)
    at rmi.Client.main(Client.java:13)

我在端口1212使用不同项目的服务器和客户端
这是服务器

package De_1;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
    public static void main(String[] args) throws RemoteException, AlreadyBoundException {
        Registry r = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        ProductDAO productDAO = new ProductDAOImpl();
        r.bind("Server", productDAO);
    }
}

这是客户

package rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Client {
    public static void main(String[] args) throws RemoteException, NotBoundException {
        Registry r = LocateRegistry.getRegistry(1212);
        ProductDAO productDAO = (ProductDAO) r.lookup("Server");

        System.out.println("Hay nhap");
        Scanner sc = new Scanner(System.in);
        while (true) {
            String command = sc.nextLine();
            StringTokenizer stringTokenizer = new StringTokenizer(command);
            String request = stringTokenizer.nextToken();
            System.out.println(request);
            switch (request) {
                case "HELLO": {
                    productDAO.addProduct();
                }
                default:
                    break;
            }
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题