java - "ClassCastException: $Proxy0 cannot be cast" error while creating simple RMI application -
i creating first, simple rmi client-server application.
here code:
interface "icommunication"
package itu.exercies.rmi.server; import java.rmi.remote; import java.rmi.remoteexception; public interface icommunication extends remote { public string docommunicate(string name) throws remoteexception; }
interface implementation "communicationimpl":
package itu.exercies.rmi.server; import java.rmi.remoteexception; import java.rmi.server.unicastremoteobject; public class communicationimpl extends unicastremoteobject implements icommunication { /** * */ private static final long serialversionuid = 1l; public communicationimpl() throws remoteexception { super(); } @override public string docommunicate(string name) throws remoteexception { return "hello server , whats " +name+ " ?!\n"; } }
here main class of server "communicationserver":
package itu.exercies.rmi.server; import java.net.malformedurlexception; import java.rmi.naming; import java.rmi.remoteexception; public class communicationserver { /** * @param args * @throws remoteexception * @throws malformedurlexception */ public static void main(string[] args) throws remoteexception, malformedurlexception { communicationimpl imp = new communicationimpl(); naming.rebind("communicationimpl", imp); system.out.println("server started..."); system.out.println("object registered. bound name 'communicationimpl'."); } }
and client "communicationclient":
package itu.exercies.rmi.client; import java.net.malformedurlexception; import java.rmi.naming; import java.rmi.notboundexception; import java.rmi.remoteexception; import itu.exercies.rmi.server.communicationimpl; public class communicationclient { public static void main(string[] args) throws malformedurlexception, remoteexception, notboundexception { string url = new string("rmi://localhost/communicationimpl"); communicationimpl comm = (communicationimpl)naming.lookup(url); string reply = comm.docommunicate("wiktor"); system.out.println(reply); } }
now when try run it:
- i navigate bin directory of project using terminal
- i run rmiregistry there
- i run communicationserver new terminal window (and prints out messages seems work)
- i open third terminal window , when try run communicationclient throws exception.
java itu.exercies.rmi.client.communicationclientexception in thread "main" java.lang.classcastexception: $proxy0 cannot cast itu.exercies.rmi.server.communicationimpl @ itu.exercies.rmi.client.communicationclient.main(communicationclient.java:14)
so far have tried fix creating stub of 'communicationimpl' object using rmic instead of '$proxy0' error refers 'communicationimpl_stub':
exception in thread "main" java.lang.classcastexception: itu.exercies.rmi.server.communicationimpl_stub cannot cast itu.exercies.rmi.server.communicationimpl @ itu.exercies.rmi.client.communicationclient.main(communicationclient.java:14)
at point have no idea errors. can give me suggestions?
replace
communicationimpl comm = (communicationimpl) naming.lookup(url);
with
icommunication comm = (icommunication) naming.lookup(url);
communicationimpl
server implementation of icommunication
. client neither knows nor cares implementation, interface.
Comments
Post a Comment