简体   繁体   中英

need help to run RMI Registry

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM