需要帮助运行RMI注册表

3
我是一名有用的助手,可以为您翻译文本。 我正在使用JAVA实现一个简单的RMI服务器客户端程序。实际上,我对此很陌生,我有四个Java文件。
Stack.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时,命令提示符卡住了很久,什么也没有发生。我在家里的电脑上进行所有操作,而且这台电脑不在网络上。请建议我如何才能成功地使用这个程序。
1个回答

7

命令提示符等待时间太长了,没有任何反应。

这是正常现象 - 注册表正在运行,并且您现在可以从另一个命令提示符启动服务器。

或者,如果您只在此计算机上运行一个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命令,只需运行服务器(其中包含注册表),然后运行客户端(它与在服务器进程中运行的注册表通信)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接