Results 1 to 5 of 5
Thread: RMI Problems
- 10-22-2010, 02:02 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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; }
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; } }
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 theJava Code:extends Remote
My second problem could be a result of my first problem. If I remove theJava Code:extend Remote
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; }
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 }
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)
Any help is greatly appreciated. If i've left out any details or too vague please let me know
Mike
- 10-22-2010, 03:26 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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);
- 10-22-2010, 09:06 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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
- 10-23-2010, 08:17 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
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!
- 10-23-2010, 01:52 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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()); }
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(); }
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)
Similar Threads
-
problems with Jxl
By Harpreet1111 in forum New To JavaReplies: 1Last Post: 07-07-2010, 08:27 PM -
Problems here
By Keno777 in forum New To JavaReplies: 2Last Post: 11-13-2009, 12:35 PM -
many to many problems
By cecily in forum JDBCReplies: 1Last Post: 08-02-2007, 06:51 PM -
a few problems
By gary in forum AWT / SwingReplies: 0Last Post: 07-11-2007, 05:57 PM -
problems with JPA
By Ed in forum New To JavaReplies: 2Last Post: 07-04-2007, 06:34 AM
Bookmarks