RMI 客户端显示 java.rmi.NotBoundException 错误

Posted

技术标签:

【中文标题】RMI 客户端显示 java.rmi.NotBoundException 错误【英文标题】:RMI client is showing java.rmi.NotBoundException error 【发布时间】:2021-12-24 08:23:03 【问题描述】:

请帮助我的代码,rmi 客户端显示错误,但服务器运行正常。 我已经运行了服务器并且它运行正常,但是客户端抛出了一个java.rmi.NotBoundException,我不明白错误是客户端代码的哪一部分

这是客户端代码

    package GUI;
    import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import rmiInterface.LoginInterface;

public class LaunchGUI 

    private JFrame frame;
    private LoginInterface myService;

    LoginGUI login;

    /**
     * Launch the application.
     */
    public static void main(String[] args) 
        EventQueue.invokeLater(new Runnable() 
            public void run() 
                try 
                    LaunchGUI window = new LaunchGUI(); // 3
                    window.frame.setVisible(true);
                 catch (Exception e) 
                    e.printStackTrace();
                
            
        );
    

    /**
     * Create the application.
     */

    String mySessionCookie = "not set";

    public LaunchGUI() 
        initialize(); // 2

    

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() 
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        try 
            myService = (LoginInterface) Naming.lookup("rmi://localhost/Service");// 1
         catch (Exception e) 
            System.out.println("A problem occured: " + e.toString());
            e.printStackTrace();
        

        JButton btnNewButton = new JButton("Sign In");
        btnNewButton.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                // login = new LoginGUI();

                String str = JOptionPane.showInputDialog("Please enter the password.");
                try 
                    String result = myService.login(str);
                    if (result.equals("wrong")) 
                        System.out.println("Wonrg Password. Try again!");
                     else 
                        mySessionCookie = result;
                        System.out.println("Your login was successful.");
                    
                 catch (Exception ex) 
                    System.out.println("A problem occured: " + ex.toString());
                    ex.printStackTrace();
                

            
        );
        btnNewButton.setBounds(24, 144, 97, 25);
        frame.getContentPane().add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Sign Out");
        btnNewButton_1.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 

                try 
                    String result = myService.logout(mySessionCookie);
                    System.out.println("Logout: " + result);
                 catch (Exception ex) 
                    System.out.println("A problem occured: " + ex.toString());
                    ex.printStackTrace();
                

            
        );
        btnNewButton_1.setBounds(297, 144, 97, 25);
        frame.getContentPane().add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("View Score");
        btnNewButton_2.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                String result;
                try 
                    result = myService.sayHello();
                    System.out.println("Result: " + result);
                 catch (RemoteException ex) 
                    System.out.println("A problem occured: " + ex.toString());
                    ex.printStackTrace();
                
            
        );
        btnNewButton_2.setBounds(157, 199, 97, 25);
        frame.getContentPane().add(btnNewButton_2);

        JButton btnNewButton_3 = new JButton("Start Game");
        btnNewButton_3.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                try 
                    String result = myService.getSecretMessage(mySessionCookie);
                    System.out.println("Result: " + result);
                 catch (Exception ex) 
                    System.out.println("A problem occured: " + ex.toString());
                    ex.printStackTrace();
                

            
        );
        btnNewButton_3.setBounds(157, 144, 97, 25);
        frame.getContentPane().add(btnNewButton_3);
    


这是 rmi 接口

    package rmiInterface;

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

public interface LoginInterface extends Remote 

    public String sayHello() throws RemoteException;

    public String login(String password) throws RemoteException;

    public String getSecretMessage(String cookie) throws RemoteException;

    public String logout(String cookie) throws RemoteException;



这是服务器代码

    package Server;

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

public class Server 

    public static void main(String[] args) 
        System.out.println("Attempting to start the Login Server...");
        try 
            Service myHello = new Service();
            Registry reg = LocateRegistry.createRegistry(1076);
            reg.rebind("Service", myHello);

            System.out.println("Service started. Welcome to the RMI Login Service!");

         catch (RemoteException e) 
            System.out.println("An error occured: " + e.toString());
            e.printStackTrace();
        
    


这是服务代码

package Server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import rmiInterface.LoginInterface;

public class Service extends UnicastRemoteObject implements LoginInterface 

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String sessionCookie = "abc" + Math.random();

    public Service() throws RemoteException
        // TODO Auto-generated constructor stub
    

    /*
     * @Override public String sayHello() throws RemoteException 
     * 
     * return "Hello! Have a good day!"; 
     */

    @Override
    public String getSecretMessage(String cookie) throws RemoteException 
        if (cookie.equals(sessionCookie)) 
            return "This is a secret message: Alice is in a relationship with Bob. It is complicated.";
         else 
            return "You must login to read this message";
        
    

    @Override
    public String login(String password) throws RemoteException 
        /*
         * Note and Warning! This setup demonstrates the interaction between cookies and
         * login. Actually security is not in the scope of this demo. Usually you
         * wouldn't hardcode a password in production code but the password were to be
         * checked up against an (encrypted) database.
         */
        if (password != null && password.equals("hello")) 
            sessionCookie = "xyz" + Math.random();
            // timeout(5000)//my code so heck recording for answer :)
            return sessionCookie;
         else 
            return "wrong";
        
    

    @Override
    public String sayHello() throws RemoteException 

        return "Hello! Have a good day!";
    

    public String logout(String cookie) throws RemoteException 
        sessionCookie = "abc" + Math.random();
        return "logout successful";
    

    /*
     * @Override public String logout(String cookie) throws RemoteException 
     * sessionCookie = "abc" + Math.random(); return "logout successful"; 
     */



此客户端显示 java.rmi.NotBoundException 错误请帮助!!!

【问题讨论】:

【参考方案1】:

您正在混合不同类型的连接。要连接到 RMI 服务,您应该配对此服务器代码:

Registry reg = LocateRegistry.createRegistry(port);
reg.rebind("Service", myHello);

带有客户端代码:

Registry reg = LocateRegistry.getRegistry(port);
Blah stub = (Blah) reg.lookup("Service");

或者,如果您已通过以下方式启动 rmiregistry

set CLASSPATH=/path/to/your-client.jar
rmiregistry <port>

那么这个服务器代码:

Naming.rebind("rmi://"+hostname+":"+port+"/"+name, stub);

由客户端代码匹配:

Blah stub = (Blah) Naming.lookup("rmi://"+hostname+":"+port+"/"+name);

【讨论】:

以上是关于RMI 客户端显示 java.rmi.NotBoundException 错误的主要内容,如果未能解决你的问题,请参考以下文章

什么是 RMI 注册表

RMI 服务器如何响应多个 RMI 客户端调用?

如何使用数组方法通过RMI从数据库的表中获取完整的数据?

RMI 客户端无法查找服务器 - java.rmi.UnmarshalException

回调中的 RMI 连接失败检测

RMI远程方法调用