• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI Problem : java.lang.ClassCastException

 
Simon John
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 -
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic