2011-07-08 6 views
0

私は、Java RMIに新しいですし、私は単に- JavaのRMI

基本的に(コードはメッセージの最後に示されている)の「Hello World」プログラムを実行しようとしています、私は、リモートクラス、リモートインタフェース、およびサーバクラスを自分のコンピュータの1つと別のコンピュータのクライアントクラスに持っています。 クライアントからサーバーに「hello」メッセージを取得しようとしています。 問題は、リモートインタフェースとスタブがクライアントと同じディレクトリになくても、クライアントをコンパイルして実行できなくなると同時に、サーバが実行できないときに実行できないということですサーバーと同じディレクトリにあるものを持っています。

javacを使ってサーバー/リモートクラス/インターフェイスをコンパイルしてから、rmicコンパイラを使用してコンパイルしました。 "rmic Hello"

私は(私はそれを分散したい理由である)私は、これは両方のコンピュータ内のすべてのファイルを持つことなく、仕事を得ることができるか事前に

感謝を疑問に思って!

コード:

リモート・インタフェース:

import java.rmi.*; 

//Remote Interface for the "Hello, world!" example. 
public interface HelloInterface extends Remote { 
    public String say() throws RemoteException; 
} 

リモートクラス:

import java.rmi.*; 
import java.rmi.server.*; 

public class Hello extends UnicastRemoteObject implements HelloInterface { 
    private String message; 

    public Hello (String msg) throws RemoteException { 
    message = msg; 
    } 

    public String say() throws RemoteException { 
    return message; 
    } 
} 

クライアント:。 インポートするjava.rmi。*;

public class Client 
{ 
    public static void main (String[] argv) 
    { 
     try 
       { 
      HelloInterface hello= (HelloInterface) Naming.lookup(host); //the string  representing the host was modified to be posted here 
      System.out.println (hello.say()); 
     } 
     catch (Exception e) 
     { 
      System.out.println ("Hello Server exception: " + e); 
     } 
    } 
} 

サーバー:

public static void main (String[] argv) { 
    try { 
     Naming.rebind ("Hello", new Hello ("Hello, world!")); 
     System.out.println ("Hello Server is ready."); 
    } catch (Exception e) { 
     System.out.println ("Hello Server failed: " + e); 
    } 
    } 

答えて

0

私の推測では、単に両方/どちらかの端で同じソースを作成することです。