Question : Running RMI on Tomcat Servlet - How do I set up RMISecurityManager?

I have a remote RMI object (Java desktop application) which I want to manipulate over RMI from an HTTP servlet in Apache Tomcat..

Eg. I am creating an RMI connection in my servlet's init() method:

 /** Connect to RMI server when servlet init() method called */
    public void init()
    {
        try {
            //intialiseRMISecurity();
            initialiseRMI();
            if(rc.connectToServer())
                RemoteServerConnection = true;
        }
        catch(Exception ex){}
    }

    /** Initialize RMI */
    private void intialiseRMISecurity()
    {
        System.setProperty("java.security.policy", "client.policy");
        if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
    }
    private void initialiseRMI() {
        String host = "localhost";
        try {

            Registry registry = LocateRegistry.getRegistry(host);
            rc = (RemoteInterface) registry.lookup("RemoteInterfaceImpl");
            System.out.println("Server object " + rc + " found");
            RMIRegistryConnection = true;
        } catch (Exception ex) {
            System.out.println("Unable to retrieve RMI object from registry.");
            ex.printStackTrace();
        }
    }

The remote object is bound to the RMI registry, but when I go to access it from the code in my Tomcat servlet, I get the following error

java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
      java.lang.ClassNotFoundException: dc.RemoteInterface (no security manager: RMI class loader disabled)
      at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
      at dcservlet.RemoteControl.initialiseRMI(RemoteControl.java:80)
      at dcservlet.RemoteControl.init(RemoteControl.java:61)
      at javax.servlet.GenericServlet.init(GenericServlet.java:212)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
      at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
      at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
      at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: dc.RemoteInterface (no security manager: RMI class loader disabled)
      at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
      at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)
      at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)
      at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)
      at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
      at java.io.ObjectInputStream.readClassDesc(Unknown Source)
      at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.readObject(Unknown Source)
      ... 16 more
:

I am running Apache Tomcat 6.0 on Windows XP Pro. I have tried adding a security manager in my code, and playing with the Catalina policy files but can't seem to get it to work. I am deploying my servlet to Tomcat from Netbeans 6.8 - the source/class files are located on my local harddrive.

Answer : Running RMI on Tomcat Servlet - How do I set up RMISecurityManager?

See, the exceptions which shows the RemoteInterface is not found to your tomcat application:
               Caused by: java.lang.ClassNotFoundException: dc.RemoteInterface

Please put all your RMI proxies (RemoteInterface) for client to /WEB-INF/classes of your web application with proper package stucture.
example: /WEB-INF/classes/dc/RemoteInterface.class
Random Solutions  
 
programming4us programming4us