Naming.lookup not able to get the RMI registry made in eclipse.
I have written two classes in separate packages of the same project. One is beinng used as the server and the other is used as the client.
I make the Registry entry in the Server Class as:
PowerServiceServer svr = new PowerServiceServer();
// ... and bind it with the RMI Registry
Naming.bind ("PowerService", svr);
And I am using this Registry in the Client class as:
PowerService service = (PowerService) Naming.lookup
("rmi://" + localhost + "/PowerService");
But in the Client class I am getting the error as "PowerService cannot be resolved to a type"
Server Class which is shown below: PowerServiceServer
Client Class which is shown below: PowerServiceClient
Can someonce please tell how to start the RMI services in Eclipse. It is possible to have a Server class and Client class in the Same Project of Java in eclipse.
Complete classes are given below.
package java.example.remoteserverclasses;
import java.math.*;
import java.rmi.*;
import java.rmi.server.*;
import java.example.interfaces.*;
public class PowerServiceServer extends UnicastRemoteObject
implements PowerService {
public PowerServiceServer () throws RemoteException
{
super();
}
// Calculate the square of a number
public BigInteger square ( int number )
throws RemoteException
{
String numrep = String.valueOf(number);
BigInteger bi = new BigInteger (numrep);
// Square the number
bi.multiply(bi);
return (bi);
}
public BigInteger power ( int num1, int num2)
throws RemoteException
{
String numrep = String.valueOf(num1);
BigInteger bi = new BigInteger (numrep);
bi = bi.pow(num2);
return bi;
}
public static void main ( String args[] ) throws Exception
{
// Assign a security manager, in the event that dynamic
// classes are loaded
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() );
// Create an instance of our power service server ...
PowerServiceServer svr = new PowerServiceServer();
// ... and bind it with the RMI Registry
Naming.bind ("PowerService", svr);
System.out.println ("Service bound....");
}
}
--
Client class is given below:
package java.example.remoteclientclasses;
import java.rmi.*;
import java.rmi.Naming;
import java.io.*;
import java.example.remoteserverclasses.*;
public class PowerServiceClient {
public static void main(String args[]) throws Exception
{
if (System.getSecurityManager() == null)
{
System.setSecurityManager
(new RMISecurityManager());
}
PowerService service = (PowerService) Naming.lookup
("rmi://" + localhost + "/PowerService");
DataInputStream din = new
DataInputStream (System.in);
for (;;)
{
System.out.println
("1 - Calculate square");
System.out.println
("2 - Calculate power");
System.out.println
("3 - Exit"); System.out.println();
System.out.print ("Choice : ");
String line = din.readLine();
Integer choice = new Integer(line);
int value = choice.intValue();
switch (value)
{
case 1:
System.out.print ("Number : ");
line = din.readLine();System.out.println();
choice = new Integer (line);
value = choice.intValue();
// Call remote method
System.out.println
("Answer : " + service.square(value));
break;
case 2:
System.out.print ("Number : ");
line = din.readLine();
choice = new Integer (line);
value = choice.intValue();
System.out.print ("Power : ");
line = din.readLine();
choice = new Integer (line);
int power = choice.intValue();
// Call remote method
System.out.println
("Answer : " + service.power(value, power));
break;
case 3:
System.exit(0);
default :
System.out.println ("Invalid option");
break;
}
}
}
}

