Search code examples
javaspringrmi

Java ClassNotFound Exception on RMI remote Server Interface


This is the code of my server main

public static void main(String Args[]){
      try {
        masterDAO = MasterDAO.getInstance();
    } catch (MasterException e) {
        e.printStackTrace();
        LOGGER.log(Level.SEVERE,e.getMessage() + "MASTER SHUTDOWN");
        System.exit(1);
    }
    final int REGISTRYPORT = 1499;
    String registryHost = "localhost";
    String serviceName = "MasterService";
    try {
        String completeName = "//" + registryHost + ":" + REGISTRYPORT + "/" + serviceName;
        System.out.println(completeName);
        MasterInterface master = (MasterInterface) new Master();
        registry = LocateRegistry.createRegistry(REGISTRYPORT);
        registry.rebind(completeName, master);
        System.out.println("Master Bound ");
    }
    catch (Exception e) {
        System.err.println("Master exception: ");
        e.printStackTrace();
    }
}

And this is a method on the client side :

public FileLocation getFileLocation(String fileName, String operation){

    final int REGISTRYPORT = 1499;
    String serviceName = "MasterService";
    String completeName = "//" + "localhost" + ":" + REGISTRYPORT + "/" + serviceName;
    try {
        System.out.println(completeName);
        Registry masterRegistry = LocateRegistry.getRegistry(1499);
        System.out.println(globalInformation.getHost() + " "+REGISTRYPORT);
        MasterInterface master = (MasterInterface) masterRegistry.lookup(completeName);
        //System.out.println("Get File Location - Result: " + fileLocation.isResult() + " - Port: "+ fileLocation.getFilePositions());
        return master.checkFile(fileName,operation);
    }
    catch (NotBoundException | IOException e) {
        e.printStackTrace();
    }  catch (FileNotFoundException e) {

        System.out.println("ERROR 404 FILE NOT FOUND");
    } catch (MasterException e) {

        System.out.println("ERROR 500 INTERNAL SERVER ERROR");
    }
    return  null;
}

I'm getting this exception

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
java.lang.ClassNotFoundException: MasterInterface (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:127)
at com.sdcc_project.cloudlet.controller.CloudLetController.getFileLocation(CloudLetController.java:125)
at com.sdcc_project.cloudlet.controller.CloudLetController.writeToMaster(CloudLetController.java:149)
at com.sdcc_project.cloudlet.CloudletApplication$3.run(CloudletApplication.java:126)
Caused by: java.lang.ClassNotFoundException: MasterInterface (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:556)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:265)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1800)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1748)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:123)
... 3 more

The server is executed locally launching its main , the client side is executed like a spring application on a tomcat server. Before deployng on spring i used to run the client like a normal application and all was working perfectly,so i think maybe the problem is on tomcat classpath? this is the the package of the project -->

enter image description here


Solution

  • Having different package name for the shared classes and interfaces (e.g. MasterInterface) on the server vs. client side application can cause this problem.

    Check both projects and make sure that it has the same naming in both. Try to achieve the same hierarchy, eg.:

    package com.xx.yy.server_intarface;  // same in server and client application
    
    public interface MasterInterface { ...
    

    This solved the problem , but not only the "package" value must be the same also all the import path.