Java 8を使用し、単純なRMIアプリケーションを作成しましたが、理解できないクライアント側の例外があります。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
コードは次のとおりです。
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
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
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
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));
}
}
動的にスタブクラスをダウンロードしたくありません。そのため、クラスのスタブとスケルを取得してから、bin \ clientにスタブクラスをコピーしました。スタブクラスとスケルクラスを取得するには、次の手順に従いました。
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
アプリケーション全体を実行するには、次の手順に従いました。
プロンプト1:
1) set classpath=C:/JavaWorkspace/RMI_project/bin
2) start rmiregistry
3) java server.MainServer
そして私はきちんと得ました:
ok1
ok2
ok3
プロンプト2による:
1) set classpath=C:/JavaWorkspace/RMI_project/bin
2) java client.MainClient
例外が発生した場所:
Exception in thread "main" java.lang.ClassCastException: server.ImplementazioneMyClassServer_Stub cannot be cast to client.InterfacciaMyClassServer
at client.MainClient.main(MainClient.java:18)
例外はMainClientの18行目ですが、何が問題なのかわかりません。
2つのパッケージで宣言されたリモートインターフェイスがあります。サーバー側はサーバーパッケージのリモートインターフェイスを使用します。クライアント側は、クライアントパッケージのリモートインターフェイスを使用します。これらの2つのリモートインターフェイスは異なります。一方の実装をもう一方にキャストすることはできません。
あなたはこれを行うことはできません。両端で、同じパッケージ内の同じリモートインターフェイスを使用する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加