线程“main”中的异常 java.rmi.NotBoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20319837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:03:32  来源:igfitidea点击:

Exception in thread "main" java.rmi.NotBoundException

javarmi

提问by Antwan

i have problem that when i run my rmi server i got an exception notbound exception even i export the remote object and bind it to registry here is my remote interface code

我有一个问题,当我运行我的 rmi 服务器时,即使我导出远程对象并将其绑定到注册表,我也遇到了异常未绑定异常,这里是我的远程接口代码

 import java.rmi.Remote;
    public interface fact extends Remote {
    public int factory(int a);
    } 

and here the interface implementation

这里是接口实现

public class factimport implements fact {

    @Override
    public int factory(int a) {
        int mult=1;
        for (int i=1;i<=a;i++)
            mult=mult*i;
        return mult;
    }

}

and server code

和服务器代码

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


public class Server extends UnicastRemoteObject {
/**
     * 
     */
    private static final long serialVersionUID = 1L;

protected Server() throws RemoteException {
        super();

    }

public static void main() throws RemoteException, MalformedURLException{
    factimport fi=new factimport();
    Registry reg=LocateRegistry.createRegistry(1099);
    reg.rebind("factobject", exportObject(fi));
    System.out.println("server started");
}
}

and client

和客户

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class Client {

    /**
     * @param args
     * @throws NotBoundException 
     * @throws RemoteException 
     * @throws MalformedURLException 
     */
    public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
        // TODO Auto-generated method stub
        Registry reg=LocateRegistry.getRegistry("127.0.0.1",1099);
    factimport x=(factimport)reg.lookup("factobject");
    System.out.println(x.factory(5));

}
}

采纳答案by user207421

  1. Unless you're running the server and client on the same host, which makes RMI pretty pointless, your getRegistry()call in the client needs to be modified to point to the server host, not the client host (itself).

  2. Your remote method must be declared to throw RemoteExceptionin the remote interface.

  3. Unless you have run rmicon the remote interface, which you can't have done or you would have detected (2), you need to change the exportObject()call to exportObject(fi, 0),for reasons explained in the preamble to the Javadoc for UnicastRemoteObject.

  4. Your Server.main()method doesn't have a correct signature so it won't execute. It should be public static void main(String[] args) ...

  5. You should make the Registry regvariable in the server static to prevent it from being garbage-collected.

  6. In your client, your variable of type factimportshould be of type fact, and you should cast the lookup result to fact.

  7. If the BindExceptionwas really the result of running this code in this state on a single machine, it can only mean that you ignored runtime exceptions when starting the server.

  1. 除非您在同一台主机上运行服务器和客户端,这使得 RMI 毫无意义,否则您在客户端中的getRegistry()调用需要修改为指向服务器主机,而不是客户端主机(本身)。

  2. 您的远程方法必须声明为抛出RemoteException远程接口。

  3. 除非您已rmic在远程接口上运行,否则您无法完成或您会检测到 (2),否则您需要更改对 Javadoc 序言中说明的原因的exportObject()调用exportObject(fi, 0),UnicastRemoteObject.

  4. 您的Server.main()方法没有正确的签名,因此不会执行。它应该是public static void main(String[] args) ...

  5. 您应该Registry reg将服务器中的变量设为静态以防止它被垃圾收集。

  6. 在您的客户端中,您的 type 变量factimport应该是 type fact,并且您应该将查找结果转换为fact.

  7. 如果BindException确实是在单台机器上以这种状态运行此代码的结果,则只能意味着您在启动服务器时忽略了运行时异常。

回答by tbodt

Your code is a little unorthodox. I don't know if this will help, but it might.

你的代码有点不正统。我不知道这是否会有所帮助,但可能会有所帮助。

Normally, the remote object implementationwould extend UnicastRemoteObject. So, you have this factimportclass:

通常,远程对象实现会扩展UnicastRemoteObject. 所以,你有这个factimport类:

public class factimport implements fact extends UnicastRemoteObject {

    @Override
    public int factory(int a) {
        int mult=1;
        for (int i=1;i<=a;i++)
            mult=mult*i;
        return mult;
    }

}

And the server class:

和服务器类:

public class Server {
/**
     * 
     */
    private static final long serialVersionUID = 1L;

protected Server() throws RemoteException {
        super();

    }

public static void main() throws RemoteException, MalformedURLException{
    factimport fi=new factimport();
    Registry reg=LocateRegistry.createRegistry(1099);
    reg.rebind("factobject", fi);
    System.out.println("server started");
}
}

I hope this will work.

我希望这会奏效。