Forums Register Login
ClassCastException Problem
I have a business interface for data access: For example

In RMI, I have the same interface, say

Then I try to connect client across the network when server running and I try and cast and use the data layer interface

I get following error:
"java.lang.ClassCastException: Proxy cannot be cast to suncertify.db.HotelReservationDB"
Any reason why I can't cast, even though interfaces are same apart from remote exception?
Hi Mark,

Look, HotelReservationDB Interface is totally different of HotelReservationDBRemote interface.
You cannot to cast like this: "(HotelReservationDB)hotelReservation". To do it, is the same you want to transform "orange" in "apple". You can't to do this. Think about it. What do you really want to do?
Cheers, thanks. Sorry, if I give impression I'm clueless, I was hoping to have cleaner code when calling the direct or network client connection by having the 1 interface reference. Is the only approach to this referencing to use a separate reference for each of the direct client and network client connection? Thanks very much for kind replies.
Howdy, Mark!

Cheers, thanks. Sorry, if I give impression I'm clueless

Champion, we are all here to discuss and help each other. So... no need to worry, partner! We are glad to help and discuss

Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.

Roberto Perillo wrote:Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.

That's what I have been dreaming about for many weeks and months: being referenced by the Great Roberto

That's my good friend Roel!
Cheers, thanks for all your help. Just wondering 1 final point, in my set-up I have, package-wise say:
BusinessService and LocalBusinessServiceImpl both in the db package.
BusinessRemote and NetworkBusinessServiceImpl in the remote package.
How can I justify throwing the remoteException for the methods in the BusinessService interface while these classes are supposed to be accessed locally? I hope one understands that I'm asking.
Or would I be better packaging the application differently to support throwing the RemoteException from the methods?
Well, for everything related to the business/services layer, I'd create a package called business or services.

Now, about the exception, I see your point... but if a method is defined in an interface and has the RemoteException in its throws clause, it means that it can throw it. The implementation doesn't need to include it in its throws clause if it won't be thrown.
Hi Mark,

It is also shown in the provided: the method in the LocalBusinessServiceImpl is not throwing a RemoteException.

Regarding your package structure. I defined 3 packages: service (contains my business interface + service-exceptions), direct (for the local business implementations) and rmi (contains all classes to do the RMI-stuff).

Kind regards,

This thread has been viewed 925 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 22, 2018 03:21:04.