wood burning stoves*
The moose likes Distributed Java and the fly likes can't get this little RMI program to run... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Distributed Java
Bookmark "can Watch "can New topic
Author

can't get this little RMI program to run...

Tobias Koch
Greenhorn

Joined: May 28, 2003
Posts: 6
Guys,
I have a little problem with this simple RMI program...

Here the 4 classes:
--------------------------------------------------------------
import java.rmi.*;

public interface remoteInterface extends Remote {
public void printLosung() throws RemoteException;
}
--------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;

public class server {
public static void main(String[] args) {
try {
remoteObject obj = new remoteObject();
Naming.rebind("Tageslosung", obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;

public class remoteObject extends UnicastRemoteObject implements remoteInterface {
public remoteObject() throws RemoteException {}
public void printLosung() {
System.out.println("text");
}
}
--------------------------------------------------------------
public class client {
public static void main(String[] args) {
//System.setSecurityManager(new RMISecurityManager());

try {
remoteObject obj = (remoteObject) Naming.lookup("rmi://localhost/Tageslosung");
obj.printLosung();
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------
After compiling, rmic-ing the remote object and registering it, I start server and client, but client gives me the message "ClassCastException" in the client class (obviously because of that remote object). But well, I don't get it really, I mean why? How should I do it then?
Thanks a lot for your help
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Tobias Koch:
remoteObject obj = (remoteObject) Naming.lookup("rmi://localhost/Tageslosung");
When working with remote objects, you should use their interfaces instead of the classes. Try this instead:The reason is that the object returned from lookup() is not a remoteObject but a new class, created by rmic, that implements remoteInterface with methods that make remote calls to the RMI server.
[ January 17, 2005: Message edited by: David Harkness ]
Tobias Koch
Greenhorn

Joined: May 28, 2003
Posts: 6
David,
thanks for your reply!
At compile-time I now get the following error message:

"remoteInterface is abstract; cannot be instantiated"

What to do?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

It sounds like you switched from the implementation to the interface in the server side code... this should be done in the client side code, not the server.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
GeeCON Prague 2014
 
subject: can't get this little RMI program to run...