问题描述
大家好,我有一项任务是制作一个 RMI 应用程序,该应用程序可以打开并读取驻留在远程服务器上的文件。我想我拥有我需要的一切(除了接口方法,我可以稍后编写)但是分配的主要规范是必须只有 两个 文件,我将其命名为 RMIFileServer 和 RMIFileClient。这意味着必须在与 RMIFileServer 相同的文件中编写和实现接口(这会导致问题)。我必须实现与此无异的接口。
public interface RemoteFileObject extends Remote{
public abstract void open(String fileName) throws remoteexception;
public abstract String readLine() throws remoteexception;
public abstract void close() throws remoteexception;
}
RMIFileServer.java(抱歉格式化,还没有弄清楚如何在此处正确发布代码) 现在我只实现接口的 readLine() 方法来测试 RMI 是否正常工作。
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class RMIFileServer extends UnicastRemoteObject {
// default constructor throwing remoteexception
public RMIFileServer() throws remoteexception{};
// interface deFinition
public interface RemoteFileObject extends Remote{
public abstract void open(String fileName) throws remoteexception;
public abstract String readLine() throws remoteexception;
public abstract void close() throws remoteexception;
}
// inner class for interface implementation
class Impl implements RemoteFileObject {
public void open(String fileName) throws remoteexception{
}
public String readLine() throws remoteexception{
return "From Server";
}
public void close() throws remoteexception{
}
}
// creation of the server
public static void main(String[] args) throws remoteexception,AlreadyBoundException{
try{
RMIFileServer fileServer = new RMIFileServer();
// remote object stub for interface methods
RemoteFileObject stub = (RemoteFileObject) UnicastRemoteObject.exportObject(fileServer,0);
// register the remote object with RMI registy
Registry registy = LocateRegistry.getRegistry();
registy.bind("RemoteFileObject",stub);
System.err.println("Server ready");
}
catch (java.io.IOException ioe){
throw new remoteexception("IO Exception",ioe);
}
}
}
RMIFileClient.java
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class RMIFileClient{
public static void main(String[] args){
String host = (args.length < 1) ? null : args[0];
try{
Registry registry = LocateRegistry.getRegistry(host);
RMIFileServer.RemoteFileObject stub = (RMIFileServer.RemoteFileObject)
registry.lookup("RMIFileServer.RemoteFileObject");
String response = stub.readLine();
System.out.println("Response: "+ response);
}
catch (Exception e){
System.err.println(e);
}
}
}
文件编译正常。尝试 rmic RMIFileServer
时我收到以下错误“只有直接实现扩展 java.rmi.Remote 的接口的类才需要存根;类 RMIFileServer 不直接实现远程接口。” 我知道这意味着 RMIFileServer 必须实现接口,但正如我所说,我不能有单独的文件来实现接口。它必须在 RMIFileServer 类中定义和实现。错误存在于 RMIFileServer 主要方法中,其中实例化了一个新的类对象。我该怎么做才能解决这个问题?
感谢您阅读到这里,感谢您的时间。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)