[JAVA]Remote Cast with RMI

yozine Posted messages 11 Registration date   Status Member -  
yozine Posted messages 11 Registration date   Status Member -
Hi, I'm having a bit of trouble running RMI with Eclipse. Any help would be greatly appreciated!

I created two packages, corresponding to the client and server of the application I want to distribute:
STEU_client
STEU_server

The server contains:
- InterfaceEau.java (remote interface)
- EauImp.java (extends UnicastRemoteObject)
- StartEau.java (starts the registry and registers the EauImpl service)
- EauImp_stub.java (generated by rmic)
+ other classes not useful here

Client contains:
- InterfaceEau.java (remote interface)
- TestRMI.java (client that calls the RMI stub)
+ ...

I generate the stub on the server side (apparently with JRMP 1.2 there is no longer a skeleton on the client side)

I set up an RMI security manager on both client and server sides.

I start my RMI registry on 'localhost:1099' and register the remote service with 'bind'. So far, so good.

Then I call the registered service with a client using 'lookup'.

I cast the Remote object returned by the name of my remote interface.

And then I have ..

.. the error in question: "java.lang.ClassCastException: rmi.steu.server.EauImpl_Stub cannot be cast to rmi.steu.client.InterfaceEau ..."

Does anyone have any idea?
Configuration: Windows XP Firefox 1.0.4

7 answers

tibo
 
Hi, I'm having a bit of trouble getting RMI to work with Eclipse. Any help would be greatly appreciated!

I've created two packages, corresponding to the client and server of the application I want to distribute:
STEU_client
STEU_server

The server contains:
- InterfaceEau.java (implements Remote)
- EauImp.java (extends UnicastRemoteObject)

{
//...
InterfaceEau serverEau =
(InterfaceEau) Naming.lookup(url);

//...
}
Is that what you have?
It's funny that it's trying to cast the stub...
Can you be a bit more specific please (a small piece of code ^^)
0
yozine Posted messages 11 Registration date   Status Member 2
 
Here's a bit of code to illustrate:

In this code, I call the remote object and cast it. That’s what it should normally do..

package rmi.steu.client; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RMISecurityManager; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class TestRMI extends UnicastRemoteObject { private InterfaceEau init; private String retourRMI; public TestRMI() throws RemoteException { try { System.setSecurityManager(new RMISecurityManager()); init = (InterfaceEau) Naming.lookup("rmi://localhost:1099/communique_etat"); retourRMI = init.getBonjour(); System.out.print(retourRMI); } catch ...
0
tibo
 
this is what should be done as a standard...
the issue may come from the RMISecurityManager
or the bind, what did you bind? an instance of EauImp.java

indeed with jdk 1.5 the skeleton is no longer present
0
yozine Posted messages 11 Registration date   Status Member 2
 
ouep here it is for the binding, all of this is in StartEau, I bind an EauImpl object:

 try { java.rmi.registry.LocateRegistry.createRegistry(1099); //System.out.println("Setting up the Security Manager ..."); System.setSecurityManager(new java.rmi.RMISecurityManager()); } catch(Exception e) { System.out.print("An RMI registry already exists.\n\n"); } try { // register the service Naming.bind("communique_etat", new EauImpl()); } catch 
0
yozine Posted messages 11 Registration date   Status Member 2
 
I've found the problem.

In fact, the client and the server need to use the same file containing the interface. Apparently, having a copy of the file containing the interface in the client project is not enough.

The cause is probably a 'classloader' issue. The ideal would be for the client to load the file containing the interface that is located on the server, using its URL. Apparently, this can be done. So I will need to work on the 'classloader' for the 'remote interface'.

All this just to display 'hello'...
0
tibo
 
Indeed, it's at the classloader level that you'll need to configure everything.

I believe there are things that can be done with the SecurityManager.

Here's something that could help you:
https://docs.oracle.com/javase/tutorial/rmi/running.html

Don't forget to post your solution's code :)
It could always be useful later on.
0
yozine Posted messages 11 Registration date   Status Member 2
 
Actually, for the classloader it doesn't seem too complicated, there's just one function to call I think, but you need to put the classes in a jar, I don't know how to do that, but that's not too serious.. I just wanted to know how RMI calls work and get it running.

Otherwise, to solve the issue, I created a common project for both the client and server projects in Eclipse, and it works well.
0