aspose file tools*
The moose likes Distributed Java and the fly likes RMI Problem : java.lang.ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "RMI Problem : java.lang.ClassCastException" Watch "RMI Problem : java.lang.ClassCastException" New topic
Author

RMI Problem : java.lang.ClassCastException

Simon John
Greenhorn

Joined: Jan 04, 2002
Posts: 20
Code Written
=============

import java.rmi.*;
/**
* Remote Interface for the "Hello, world!" example.
*/
public interface HelloInterface extends Remote {
/**
* Remotely invocable method.
* @return the message of the remote object, such as "Hello, world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException;
}

===============

import java.rmi.*;
import java.rmi.server.*;
/**
* Remote Class for the "Hello, world!" example.
*/
public class Hello extends UnicastRemoteObject implements HelloInterface {
private String message;
/**
* Construct a remote object
* @param msg the message of the remote object, such as "Hello, world!".
* @exception RemoteException if the object handle cannot be constructed.
*/
public Hello (String msg) throws RemoteException {
message = msg;
}
/**
* Implementation of the remotely invocable method.
* @return the message of the remote object, such as "Hello, world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException {
return message;
}
}
================
import java.rmi.*;

public class HelloClient
{
/**
* Client program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
public static void main (String[] argv) {
try {
HelloInterface hello =
(HelloInterface) Naming.lookup ("//localhost:1099");
System.out.println (hello.say());
} catch (Exception e) {
System.out.println ("HelloClient exception: " + e);
}
}



}
===========
import java.rmi.*;

public class HelloServer
{

/**
* Server program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
public static void main (String[] argv) {
try {
Naming.rebind ("Hello", new Hello ("Hello, world!"));
System.out.println ("Hello Server is ready.");
} catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
=================


Then followings steps was followed :
javac HelloInterface.java
javac Hello.java
rmic Hello
rmiregistry&

Then on other command line console
javac HelloServer.java
java HelloServer &

Then on other command line console
javac HelloClient.java
java HelloClient

Then got the following exception
HelloClient exception: java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub


All the files are in same working directory, Please help me out to resolve this problem.

Regards
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You're getting a ClassCastException because you are getting a reference to the naming registry itself, and then trying to cast it to a HelloInterface-



To get a reference to your HelloInterface you need to look up the name that it is bound under -


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Simon (and Nate),
The lookup string used by the client in the "Naming.lookup()" method is a URL and a URL also includes a protocol. In your case, Simon, the protocol is "rmi". If your (RMI) client and (RMI) server are both running on the same machine (which seems to be the case, according to your description), then you can use "localhost" for the host name -- otherwise you need to use the actual host name (or IP address). Also, if you are only running one "rmiregistry" process, then you do not need to include the port number in your (lookup) URL. Therefore, in my opinion, the following should suffice:

rmi://localhost/Hello

Good Luck,
Avi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RMI Problem : java.lang.ClassCastException