HI,
I am learning RMI from Sun site. I am trying to do Calculator example but getting error.I think there is something I am missing in naming statement.Please help me out.
Here is code,
* Definition of Remote Service (Interface) **/
public interface Calculator extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
/** Implemamtation of Remote Service **/
public class CalculatorImpl
extends java.rmi.server.UnicastRemoteObject implements Calculator {
public CalculatorImpl ( ) throws java.rmi.RemoteException {
super();
}
public long add (long a , long b) throws java.rmi.RemoteException {
return a + b;
}
public long sub (long a , long b) throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a , long b) throws java.rmi.RemoteException {
return a * b;
}
public long div (long a , long b) throws java.rmi.RemoteException {
return a / b;
}
}
These two programs working fine.Created skeleton and stub class.
/**
Remote RMI services must be hosted in a server process. The class CalculatorServer is a very simple server that provides the bare essentials for hosting. **/
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer () {
try {
Calculator c =new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
} catch(Exception e){
System.out.println("Troubel !!! " +e.getMessage()) ;
}
}
public static void main(
String arg[] ) {
new CalculatorServer ();
}
}
/**
Client program
**/
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main (String arg[] ) {
try {
Calculator c =(Calculator) Naming.lookup( "rmi://remotehost/CalculatorService ");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}catch (MalformedURLException murle) {
System.out.println();
System.out.println "MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException")
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
I am running RMI system with 3 different console.
1)After >rmiregistry camand .it doesn't open any other window just hangs at camand promt.
2) when try to run CalculatorServer program it's giving folowing exception error
C:\RMI\Calculator>
JAVA CalculatorServer
Troubel !!! RemoteException occurred in server
thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalculatorImpl_Stub
3)For Client program giving Error as ,
RemoteException
java.rmi.UnknownHostException: Unknown host: remotehost; nested exception is:
java.net.UnknownHostException: remotehost
DO I need to do some classpath settings?I will appreciate your feedback.
Thanks,
Padmashree
[This message has been edited by padmshree Patil (edited August 27, 2001).]