java - RMI: ClassNotFoundException thrown while binding two remote objects to same registry -
i have 2 remote objects i'm trying bind same rmi registry. here's project structure:
my first class(remoteserver1.java)
package com.abc.rmi; import java.rmi.accessexception; import java.rmi.alreadyboundexception; import java.rmi.remote; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; import org.apache.log4j.logger; public class remoteserver1 implements iserver1interface{ private logger logger = logger.getlogger(remoteserver1.class); public void init(){ registry localregistry = null; iserver1interface server1 = null; try { int port = 1099; logger.info("service property rmiregistryport: " + port); server1 = (iserver1interface)unicastremoteobject.exportobject(this, 0); logger.info("locating registry"); localregistry = locateregistry.getregistry(); try { localregistry.list(); } catch (remoteexception localremoteexception2) { logger.info("creating registry"); try { localregistry = locateregistry.createregistry(port); } catch (remoteexception localremoteexception4) { logger.error("server1 remoteexception. " + localremoteexception4); throw new runtimeexception("server1 - registry creation failed. ", localremoteexception4); } } logger.info("binding"); localregistry.bind("server1", server1); } catch (alreadyboundexception localalreadyboundexception) { logger.warn("server1 - object bound." + localalreadyboundexception); try { logger.info("re-binding"); localregistry.rebind("server1", server1); } catch (accessexception localaccessexception) { throw new runtimeexception("server1 - accessexception while re-binding. ", localaccessexception); } catch (remoteexception localremoteexception3) { throw new runtimeexception("server1 - remoteexception while re-binding. ", localremoteexception3); } } catch (remoteexception localremoteexception1) { logger.error("server1 remoteexception. " + localremoteexception1); throw new runtimeexception("server1 - remoteexception. ", localremoteexception1); } logger.info("remote server1 ready"); } }
here's second class(remoteserver2.java)
package com.abc.rmi2; import java.rmi.accessexception; import java.rmi.alreadyboundexception; import java.rmi.remote; import java.rmi.remoteexception; import java.rmi.registry.locateregistry; import java.rmi.registry.registry; import java.rmi.server.unicastremoteobject; import org.apache.log4j.logger; public class remoteserver2 implements iserver2interface{ private logger logger = logger.getlogger(remoteserver2.class); public void init(){ registry localregistry = null; iserver2interface server2 = null; try { int port = 1099; logger.info("service property rmiregistryport: " + port); server2 = (iserver2interface)unicastremoteobject.exportobject(this, 0); logger.info("locating registry"); localregistry = locateregistry.getregistry(); try { localregistry.list(); } catch (remoteexception localremoteexception2) { logger.info("creating registry"); try { localregistry = locateregistry.createregistry(port); } catch (remoteexception localremoteexception4) { logger.error("server2 remoteexception. " + localremoteexception4); throw new runtimeexception("server2 - registry creation failed. ", localremoteexception4); } } logger.info("binding"); localregistry.bind("server2", server2); } catch (alreadyboundexception localalreadyboundexception) { logger.warn("server2 - object bound." + localalreadyboundexception); try { logger.info("re-binding"); localregistry.rebind("server2", server2); } catch (accessexception localaccessexception) { throw new runtimeexception("server2 - accessexception while re-binding. ", localaccessexception); } catch (remoteexception localremoteexception3) { throw new runtimeexception("server2 - remoteexception while re-binding. ", localremoteexception3); } } catch (remoteexception localremoteexception1) { logger.error("server2 remoteexception. " + localremoteexception1); throw new runtimeexception("server2 - remoteexception. ", localremoteexception1); } logger.info("remote server2 ready"); } }
iserver1interface , iserver2interface implement java.rmi.remote. class remoteserver1.java able register rmi registry, following exception when line "localregistry.rebind("server2", server2)" called in second class(remoteserver2.java):
03-06-2014 18:03:43 error remoteserver2: - server2 remoteexception. java.rmi.serverexception: remoteexception occurred in server thread; nested exception is: java.rmi.unmarshalexception: error unmarshalling arguments; nested exception is: java.lang.classnotfoundexception: com.abc.rmi2.iserver2interface (no security manager: rmi class loader disabled)
i'm running inside tomcat server using following code:
url url = new url("file:///c:/temp/rmi/rmi1.jar"); url[] urls = {url}; urlclassloader child = new urlclassloader(urls, this.getclass().getclassloader()); class classtoload = class.forname ("com.abc.rmi.remoteserver1", true, child); method method = classtoload.getdeclaredmethod ("init"); object instance = classtoload.newinstance(); object result = method.invoke(instance); url url2 = new url("file:///c:/temp/rmi/rmi2.jar"); url[] urls2 = {url2}; urlclassloader child2 = new urlclassloader(urls2, this.getclass().getclassloader()); class classtoload2 = class.forname("com.abc.rmi2.remoteserver2", true, child2); method method2 = classtoload2.getdeclaredmethod("init"); object instance2 = classtoload2.newinstance(); object result2 = method2.invoke(instance2);
i haven't defined security manager while starting tomcat , haven't made changes tomcat security policy. don't understand how able bind first remote class fine, not second one. i've read in many places security manager must defined rmi work, able load first class without issues. why problem occur when try bind second class. read property 'java.rmi.server.codebase', i'm not sure how first 1 got loaded though didn't specify property. suggestions?
the registry doesn't have second class available via classpath or via codebase feature. there wrong 1 of those.
Comments
Post a Comment