如何实现基于RMI远程服务器接口的JavaClassNotFound异常?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (116)

这是我服务器主的代码

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();
    }
}

这是客户端的一种方法:

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;
}

我得到了这个例外

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

服务器在本地执行启动其主服务器,客户端执行就像tomcat服务器上的spring应用程序一样。在springng上部署之前我曾经像普通的应用程序一样运行客户端而且一切都运行得很好,所以我想也许问题出在tomcat classpath上?这是项目的包 - >

提问于
用户回答回答于

MasterInterface服务器端与客户端应用程序上具有不同的共享类和接口(例如)的包名称可能会导致此问题。

检查两个项目并确保它们具有相同的命名。尝试实现相同的层次结构,例如:

package com.xx.yy.server_intarface;  // same in server and client application

public interface MasterInterface { ...

扫码关注云+社区

领取腾讯云代金券