need help to run RMI Registry

China☆狼群 提交于 2020-01-02 07:33:19

问题


I'm implementing a simple RMI Server Client program in JAVA. I'm new to this actually. i have four java files.

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());

}
}

I'm using JDK1.5. The sequence in which i compiled the files is, first i compiled Stack.java then i compiled StackImp.java then i used this command rmic StackImp this all was successful. But when i tried to run the registry this way rmiregistery 2000, command prompt took too long. Nothing happened. I'm doing this all at my home PC. And this PC is not on the network. Please suggest me what to do to successfully work with this program.


回答1:


command prompt took too long. Nothing happened.

Nothing is supposed to happen - the registry is running, and you can now start your server from another command prompt.

Alternatively, if you're only running the one RMI server process on this machine you can run the registry in the same process as the RMI server:

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...");   

  }
}

This way you don't need a separate rmiregistry command, just run the server (which includes the registry) and then the client (which talks to the registry that is running in the server process).



来源:https://stackoverflow.com/questions/14323991/need-help-to-run-rmi-registry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!