Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tobias Koch
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic