java.rmi.NoSuchObjectException但添加了静态引用

java.rmi.NoSuchObjectException但添加了静态引用,java,exception,rmi,Java,Exception,Rmi,我正在用java RMI实现一个客户机/服务器应用程序,客户机和服务器在同一个项目中的不同包中(当然,它们在同一台机器上运行),接口在一个“公共”包中定义。当我尝试获取两个远程对象中的一个时,我得到了前面提到的例外。我读过一些SO的答案,人们建议给远程对象添加一个静态引用,这样它们就永远不会被收集,但这是行不通的 这是客户端和服务器的代码 通知服务代理 public class NotificheServiceProxy implements INotificheService { @

我正在用java RMI实现一个客户机/服务器应用程序,客户机和服务器在同一个项目中的不同包中(当然,它们在同一台机器上运行),接口在一个“公共”包中定义。当我尝试获取两个远程对象中的一个时,我得到了前面提到的例外。我读过一些SO的答案,人们建议给远程对象添加一个静态引用,这样它们就永远不会被收集,但这是行不通的

这是客户端和服务器的代码

通知服务代理

public class NotificheServiceProxy implements INotificheService
{

    @Override
    public ArrayList <NotificaDTO> generaNotifiche(String autostrada, Date data, Date oraInizio, Date oraFine)
    {
        try 
        {
            RMIClient client = new RMIClient ("localhost" , 1099);
            INotificheService service = (INotificheService) client.getService(INotificheService.class);
            return service.generaNotifiche(autostrada, data, oraInizio, oraFine);
        }
        catch (RemoteException ex)
        {
            Logger.getLogger(TrattaServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        catch (NotBoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
通知服务(服务器端)

每个接口都使用@RMIMapping注释进行注释,该注释提供对象绑定名称。但我相信问题不在于此

这对我来说是一个真正的迷雾,这是stacktrace……非常感谢你的帮助

ott 03, 2014 5:13:17 PM it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy generaNotifiche
SEVERE: null
java.rmi.NoSuchObjectException: no such object in table
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy3.generaNotifiche(Unknown Source)
    at it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy.generaNotifiche(NotificheServiceProxy.java:25)
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:17)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:20)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
编辑:@EJP在此之前,我做过类似的事情

public class RemoteObject implements IRemoteObject
{
    private static RemoteObject instance;

    public RemoteObject ()
    {
       instance = this;
    }

    public someType someMethod (...)
}
但也有同样的例外。此外,如您所见,在scServerConfig中有一个 公共静态服务器

它封装了注册表引用。如果我所做的这些事情是正确的,我认为添加静态引用可能是不够的,甚至艰难的“突然”开始对我起作用。我错了吗

java.rmi.NoSuchObjectException但添加了静态引用

它没有被添加。您正在尝试调用添加它的远程方法。你在这方面失败了,所以还没有发生

您需要在远程对象的构造函数中设置静态实例。在远程方法中这样做可能已经太晚了


我建议您也将注册表保存在静态变量服务器端。事实上,严格地说,这是保持静态的唯一参考。

这是大量的代码转储。请尽量使问题简洁。您可以阅读我已经删除了javadoc注释和不必要的代码,对不起@sscnapoli1926包括堆栈跟踪please@MarkW我做到了!它起作用了!!!!!!你救了我的考试,伙计!但在过去的一次尝试中,我已经将实例集放入构造函数中,它是无用的……而且,我从来都不需要这样做……无论如何,非常感谢!伟大的打勾或投票可能是合适的。上次你一定做错了。
public class SCServerConfig
{
    public static RMIServer server;
    public static void main (String[] args)
    {
         server = new RMIServer(1099);

        //avvia il server ed esporta oggetti remoti
        server.start().export(TrattaService.class).export(NotificheService.class);

        //Other stuff


RMIServer (starts server and provide method for remote objects exporting

public class RMIServer
{

    private Registry reg;
    private int regPort;



    public RMIServer (int regPort)
    {
        this.regPort = regPort;
    }

    public RMIServer start()
    {
        try
        {
            reg = LocateRegistry.createRegistry(regPort);
            System.out.println("SERVER OK");

        }
        catch (RemoteException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        return this;
    }

    public RMIServer export (Class object)
    {
        try
        {

            RMIMapping RMIMappingAnn = (RMIMapping) object.getInterfaces()[0].getAnnotation(RMIMapping.class);
            Remote tmp = UnicastRemoteObject.exportObject((Remote)object.newInstance(), 0);

            reg.rebind( RMIMappingAnn.name() , tmp );

            System.out.println("oggetto remoto esportato " + object);

        } 
        catch (InstantiationException ex)
        {
            System.err.println("InstantiationExeception!!\ncause:\n 1. il costruttore dell' oggetto remoto non deve avere argomenti");
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (RemoteException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }


        return this;
    }

 }
ott 03, 2014 5:13:17 PM it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy generaNotifiche
SEVERE: null
java.rmi.NoSuchObjectException: no such object in table
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy3.generaNotifiche(Unknown Source)
    at it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy.generaNotifiche(NotificheServiceProxy.java:25)
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:17)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:20)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
public class RemoteObject implements IRemoteObject
{
    private static RemoteObject instance;

    public RemoteObject ()
    {
       instance = this;
    }

    public someType someMethod (...)
}