开发者

How to export files from server to client - Java RMI

开发者 https://www.devze.com 2023-03-18 04:37 出处:网络
I am new to Java RMI and I am simply trying to run a \"Hello World\" program (code is shown at the end of the message)

I am new to Java RMI and I am simply trying to run a "Hello World" program (code is shown at the end of the message)

Basically, I have a remote class, a remote interface, and a server class in one of my computers and a client class in another computer. I am trying to get a "hello" message from the server using the client开发者_C百科. The problem is that I cannot compile the client and get it running if I don't have the remote interface and the stub in the same directory where the client is, and at the same time I cannot run the server if I don't have those in the same directory that the server is.

I compiled the server/remote class/interface using javac and then using the rmic compiler. "rmic Hello".

I am wondering how I could get this to work without having to have all the files in both computers (which is why I want to make it distributed)

Thanks in advance!

Code:

Remote Interface:

import java.rmi.*;  

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

Remote class:

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

Client: import 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);  
        }  
    }  
} 

Server:

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


My guess would be to simply create identical source at both / either end.


am wondering how I could get this to work without having to have all the files in both computers

You can't. You have to distribute the required class files to the client.

(which is why I want to make it distributed)

Non sequitur.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号