简体   繁体   English

具有客户端异常的简单RMI应用程序

[英]Simple RMI application with client-side exception

I use Java 8 and I created a simple RMI application but I have a client-side exception that I don't understand. 我使用Java 8,并创建了一个简单的RMI应用程序,但是有一个我不理解的客户端异常。 Using Eclipse the structure of my application is: 使用Eclipse,我的应用程序结构为:

---RMI_project     
-----bin
-------client
----------ImplementazioneMyClassServer_Stub.class
----------InterfacciaMyClassServer.class
----------MainClient.class
-------server
----------ImplementazioneMyClassServer.class
----------InterfacciaMyClassServer.class
----------ImplementazioneMyClassServer_Stub.class
----------ImplementazioneMyClassServer_Skel.class
----------MainServer.class
-----src 
-------client
----------InterfacciaMyClassServer.java
----------MainClient.java
-------server
----------ImplementazioneMyClassServer.java
----------InterfacciaMyClassServer.java
----------MainServer.java

here's the code: 这是代码:

InterfacciaMyClassServer.java InterfacciaMyClassServer.java

package client;

import java.rmi.*; // necessaria per estendere interfaccia Remote

public interface InterfacciaMyClassServer extends Remote {
 int somma(int a, int b) throws RemoteException;        //unico metodo
}

ImplementazioneMyClassServer.java ImplementazioneMyClassServer.java

package server;

import java.rmi.*;
import java.rmi.server.*;  // per utilizzare UnicastRemoteObject

public class ImplementazioneMyClassServer extends UnicastRemoteObject implements InterfacciaMyClassServer{

    private static final long serialVersionUID = 1L;

    public ImplementazioneMyClassServer() throws RemoteException {      // costruttore
        System.out.println("ok1");
    }

    public int somma(int a, int b) throws RemoteException{      // implementazione metodo somma definito dall'interfaccia
        return a+b;
    }
}

MainServer.java MainServer.java

package server;

import java.rmi.*;

public class MainServer {

    public static void main(String[] args) {

        try{
            ImplementazioneMyClassServer s1 = new   ImplementazioneMyClassServer();

            System.out.println("ok2");

            Naming.rebind("oggetto1", s1);

            System.out.println("ok3");
        }
        catch(Exception e){
            System.out.println(e);
        }
    }

}

MainClient.java MainClient.java

package client;

import java.net.MalformedURLException;
import java.rmi.*;

public class MainClient {

    public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {

        String nomeServer="localhost";        // nome del server o suo    indirizzo IP (per l'esempio localhost=127.0.0.1)
        String nomeOggettoRemoto="oggetto1";   // nome dell'oggetto remoto da richiedere al server 
        String protocollo="rmi";              //protocollo usato, può essere: rmi,ftp,http
        String URLoggettoRemoto=protocollo+"://"+nomeServer+"/"+nomeOggettoRemoto;  // URL completo usato dal client per ottenere riferimento oggetto remoto sul server 

        // cerca all'URL specificato all'interno del registro rmi l'oggetto remoto con nome "oggetto1"
        // e restituisce nella var oggetto il suo riferimento per cui nelle istruzioni successive
        // è possibile riferirsi all'oggetto remoto come se fosse sul client.
        InterfacciaMyClassServer oggetto = (InterfacciaMyClassServer) Naming.lookup(URLoggettoRemoto);

        // uso oggetto remoto
        System.out.println(oggetto.somma(2, 3));
    }

}

I do not want to download dynamically stub class. 我不想动态下载存根类。 That is why I got the class stub and skel and then I copied the stub class in bin\\client. 这就是为什么我得到了存根和存根类,然后将存根类复制到bin \\ client中的原因。 To get the stub and skel classes I followed the following procedure: 要获取存根和存根类,我遵循以下过程:

1) set classpath=C:\JavaWorkspace\RMI_project\src
2) cd C:\JavaWorkspace\RMI_project\src
3) C:\JavaWorkspace\RMI_project\src>javac server/InterfacciaMyClassServer.java
4) C:\JavaWorkspace\RMI_project\src>javac  server/ImplementazioneMyClassServer.java
5) C:\JavaWorkspace\RMI_project\src>rmic -v1.1 server.ImplementazioneMyClassServer

To run the entire application I followed the procedure: 要运行整个应用程序,请按照以下步骤操作:

by prompt1: 通过提示符1:

1) set classpath=C:/JavaWorkspace/RMI_project/bin
2) start rmiregistry
3) java server.MainServer

and I got properly: 我正确地:

ok1
ok2
ok3

by prompt2: 通过提示符2:

1) set classpath=C:/JavaWorkspace/RMI_project/bin
2) java client.MainClient

Where I got the exception: 我哪里有例外:

Exception in thread "main" java.lang.ClassCastException: server.ImplementazioneMyClassServer_Stub cannot be cast to client.InterfacciaMyClassServer 
at client.MainClient.main(MainClient.java:18)

The exception is the line 18 in MainClient but I do not understand what is wrong. MainClient中的第18行是一个例外,但是我不明白这是什么错误。

You have the remote interface declared in two packages. 您在两个程序包中声明了远程接口。 The server side uses the server package's remote interface; 服务器端使用服务器软件包的远程接口。 the client side uses the client package's remote interfaces. 客户端使用客户端程序包的远程接口。 These two remote interfaces are different. 这两个远程接口是不同的。 You can't cast an implementation of one to the other. 您不能将一个实现转换为另一个实现。

You can't do this. 你做不到 You need to use the same remote interface, in the same package, at both ends. 您需要在两端使用相同的远程接口,并且使用相同的程序包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM