Results 1 to 5 of 5

Thread: RMI Problems

  1. #1
    michaeln31 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default RMI Problems

    Hi,

    I've been trying to get an rmi client/server running and I keep coming back to the same problems. I've created the project in NetBeans and i have the client in one package and the server is in another package. My first problem is with the server.

    Here is my code:

    Server Package:

    My interface

    Java Code:
    package server;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    
    public interface Message extends Remote{
    
        public String getMessage() throws RemoteException;
        
        public void setMessage(String message) throws RemoteException;
    }
    My Implementation

    Java Code:
    package server;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    
    public class MessageImpl extends UnicastRemoteObject implements Message {
    
        private String message;
    
        public MessageImpl() throws RemoteException{
            super();
        }
        public String getMessage() throws RemoteException {
            return message;
        }
    
        public void setMessage(String message) throws RemoteException {
            this.message = message;
        }
    }
    And the server

    Java Code:
    package server;
    
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    public class MessageServer{
    
       public static void main(String args[])
    {
       
            try {
                MessageImpl server = new MessageImpl();
                Naming.rebind("rmi://localhost/MESSAGE_SERVER", server);
    
                System.out.println("RUNNING!!!");
            }
            catch (java.net.MalformedURLException e) {
                System.out.println("Malformed URL for MessageServer name " + e.toString());
            }
    
            catch (RemoteException e) {
                System.out.println("Communication error " + e.toString());
            }
    
    
    }
    
    }

    When i run this package i get an error:

    Java Code:
    Communication error java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
            java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
            java.lang.ClassNotFoundException: server.Message

    However if i take out the
    Java Code:
    extends Remote
    from the interface class it runs fine. I'm not too sure if that is the correct way of doing it or if i'm just "silencing" the problem and removing the "extends Remote" lets the code compile but does not create a server.


    My second problem could be a result of my first problem. If I remove the
    Java Code:
    extend Remote
    from the interface in the server package and get the server running (or what i think is running) i move onto the client package. Here is my code:

    Client interface
    Java Code:
    package client;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    
    public interface Message extends Remote{
    
        public String getMessage() throws RemoteException;
        
        public void setMessage(String message) throws RemoteException;
    }
    here is the client:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * GUI.java
     *
     * Created on 21-Oct-2010, 12:01:26
     */
    package client;
    
    import java.rmi.Naming;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    
    public class GUI extends javax.swing.JFrame {
    
        private Message client;
        Remote remoteObject;
        String name = "rmi://127.0.0.1/MESSAGE_SERVER";
    
        /** Creates new form GUI */
        public GUI() {
            
    
            try
            {
    
                remoteObject = Naming.lookup(name);
    
                client = (Message)remoteObject;
                initComponents();
            } catch (Exception e) {
                System.out.println("Error Finding MESSAGE_SERVER\n");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
    
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            lblMessage = new javax.swing.JLabel();
            cmdGet = new javax.swing.JButton();
            txtMessage = new javax.swing.JTextField();
            cmdSetMessage = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            cmdGet.setText("Get Message");
            cmdGet.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdGetActionPerformed(evt);
                }
            });
    
            cmdSetMessage.setText("Set Message");
            cmdSetMessage.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdSetMessageActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdGet, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(32, 32, 32)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtMessage)
                        .addComponent(cmdSetMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(160, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdGet)
                        .addComponent(cmdSetMessage))
                    .addContainerGap(231, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void cmdGetActionPerformed(java.awt.event.ActionEvent evt) {                                       
           try {
                lblMessage.setText(client.getMessage());
            } catch (RemoteException ex) {
                System.out.println("Error Getting Message");
            }
        }                                      
    
        private void cmdSetMessageActionPerformed(java.awt.event.ActionEvent evt) {                                              
            try {
                client.setMessage(txtMessage.getText());
            } catch (RemoteException ex) {
                System.out.println("Error Setting Message");
            }
        }                                             
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new GUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton cmdGet;
        private javax.swing.JButton cmdSetMessage;
        private javax.swing.JLabel lblMessage;
        private javax.swing.JTextField txtMessage;
        // End of variables declaration                   
    }
    When i run this, an exception is caught and prints out:

    Java Code:
    Error Finding MESSAGE_SERVER
    
    $Proxy0 cannot be cast to client.Message
    java.lang.ClassCastException: $Proxy0 cannot be cast to client.Message
            at client.GUI.<init>(GUI.java:37)
            at client.GUI$3.run(GUI.java:132)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    I am stuck as to why this is happening. I'm not sure whether it is the code that is at fault or the way i run the rmiregistry.

    Any help is greatly appreciated. If i've left out any details or too vague please let me know

    Mike

  2. #2
    Kowsi is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    U just change 127.0.0.1 instead of localhost in server program.. i think u will get..
    In server program :

    Naming.rebind("rmi://127.0.0.1/MESSAGE_SERVER", server);

  3. #3
    michaeln31 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks for the reply Kowsi but i tried that and didnt help. At my best guess I think the client error that i'm getting is due to a miss match i.e. i'm trying to fit a square block into a round hole but i don't know enough about UnicastRemoteObjects, etc to attempt to fix it. Any other idea?

    Mike

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    It looks to me like the rebind is failing. Removing 'extends Remote' is certainly silencing the error without removing it, though I don't know why.

    You might want to try using Registry and LocateRegistry, and see if you can get it working using those classes. They simplify making Remote objects available for connection, and the lookup/binding/registry creation methods are quite simple. In fact, IIRC, all the Sun Tutorials on RMI use Registries, because of the sheer difficulty of using raw Remote objects.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    michaeln31 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    I've just tried that idea but I dont think i'm using the technique wrong. I've changed my server code to this:

    Java Code:
     try
            {
                LocateRegistry.createRegistry(1099);
                Message server = new MessageImpl();
                Naming.bind("MESSAGE_SERVER", server);
    
                System.out.println("Running!!!");
    
            } catch (MalformedURLException e)
            {
            System.out.println("Malformed URL for MessageServer name " + e.toString());
            }
            catch (RemoteException e) {
                System.out.println("Communication error " + e.toString());
            }
            catch(AlreadyBoundException e)
            {
                System.out.println("Already Bound Error " + e.toString());
            }
    and it runs fine. Although i do not need to run rmiregistry from the command line, which i think is due to the ".createRegistry" method. If you can confirm that please do.

    My problem once again is on the client side. I've changed my code to this:

    Java Code:
     try {
    
                Registry reg = LocateRegistry.getRegistry("127.0.0.1");
                client = (Message) reg.lookup("MESSAGE_SERVER");            
    
                System.out.println("Connected!!!");
    
                initComponents();
            } catch (Exception e) {
                System.out.println("Error Finding MESSAGE_SERVER");
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
    I've tried several different ways of but always get different errors. Using this code gives me the same error as before

    Java Code:
    Error Finding MESSAGE_SERVER
    $Proxy0 cannot be cast to client.Message
    java.lang.ClassCastException: $Proxy0 cannot be cast to client.Message
            at client.GUI.<init>(GUI.java:37)
            at client.GUI$3.run(GUI.java:135)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Could it be my PC rather than the code thats effecting the program?

Similar Threads

  1. problems with Jxl
    By Harpreet1111 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 07:27 PM
  2. Problems here
    By Keno777 in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 11:35 AM
  3. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  4. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •