简体   繁体   English

如何将文件从服务器导出到客户端 - Java RMI

[英]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)我是 Java RMI 的新手,我只是想运行一个“Hello World”程序(代码显示在消息的末尾)

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.基本上,我的一台计算机上有一个远程 class、一个远程接口和一个服务器 class,另一台计算机上有一个客户端 class。 I am trying to get a "hello" message from the server using the client.我正在尝试使用客户端从服务器获取“hello”消息。 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.我使用 javac 编译了服务器/远程类/接口,然后使用 rmic 编译器。 "rmic Hello". “rmic 你好”。

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:远程 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.*;客户端:导入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.您必须将所需的 class 文件分发给客户端。

(which is why I want to make it distributed) (这就是为什么我想让它分发)

Non sequitur.不合逻辑的推论。

My guess would be to simply create identical source at both / either end.我的猜测是简单地在两端/两端创建相同的源。

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

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