我正在用 JAVA 实现一个简单的 RMI 服务器客户端程序。实际上我是新手。我有四个java文件。
堆栈.java
import java.rmi.*;
public interface Stack extends Remote{
public void push(int p) throws RemoteException;
public int pop() throws RemoteException;
}
StackImp.java
import java.rmi.*;
import java.rmi.server.*;
public class StackImp extends UnicastRemoteObject implements Stack{
private int tos, data[], size;
public StackImp()throws RemoteException{
super();
}
public StackImp(int s)throws RemoteException{
super();
size = s;
data = new int[size];
tos=-1;
}
public void push(int p)throws RemoteException{
tos++;
data[tos]=p;
}
public int pop()throws RemoteException{
int temp = data[tos];
tos--;
return temp;
}
}
RMIServer.java
import java.rmi.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Naming.rebind("rmi://localhost:2000/xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
RMIClient.java
import java.rmi.*;
public class RMIClient{
public static void main(String[] argv)throws Exception{
Stack s = (Stack)Naming.lookup("rmi://localhost:2000/xyz");
s.push(25);
System.out.println("Push: "+s.push());
}
}
我正在使用 JDK1.5。我编译文件的顺序是,首先我编译 Stack.java 然后我编译 StackImp.java 然后我使用这个命令 rmic StackImp 这一切都是成功的。但是当我尝试以这种方式运行注册表时 rmiregistery 2000,命令提示符花费的时间太长。什么都没发生。我在家里的电脑上做这一切。而且这台电脑不在网络上。请建议我如何成功使用该程序。
最佳答案
command prompt took too long. Nothing happened.
什么都不应该发生 - 注册表正在运行,您现在可以从另一个命令提示符启动您的服务器。
或者,如果您只在这台机器上运行一个 RMI 服务器进程,您可以在与 RMI 服务器相同的进程中运行注册表:
import java.rmi.*;
import java.rmi.registry.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Registry reg = LocateRegistry.createRegistry(2000);
reg.rebind("xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
这样您就不需要单独的 rmiregistry
命令,只需运行服务器(包括注册表),然后运行客户端(与服务器进程中运行的注册表对话) .
关于java - 需要帮助来运行 RMI Registry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14323991/