简体   繁体   中英

How to export files from server to client - Java RMI

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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