如何将文件从服务器导出到客户端 - Java RMI的
问题描述:
我是新来的Java RMI,我只是试图运行“Hello World”程序(代码显示在邮件的末尾)如何将文件从服务器导出到客户端 - Java RMI的
基本上,我在我的一台计算机中有一个远程类,一个远程接口和一个服务器类,另一台计算机中有一个客户端类。 我想从使用客户端的服务器获取“hello”消息。 问题是如果我没有远程接口和客户端所在的同一目录下的存根,我无法编译客户端并使其运行,并且同时如果我不能运行服务器有那些在服务器相同的目录中。
我使用javac编译服务器/远程类/接口,然后使用rmic编译器。 “rmic你好”。
我想知道我怎么能得到这个,而不必在两台计算机上的所有文件(这就是为什么我希望把它分发)
提前感谢合作!
代码:
远程接口:
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);
}
}
答
我的猜测是简单地创建在两个/两端相同的源。