This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RMI problem" Watch "RMI problem" New topic
Author

RMI problem

padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
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 waits at command promt.
2) when try to run CalculatorServer program it's giving folowing exception error
C:\RMI\Calculator>JAVA CalculatorServer
It also waits at command promt
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
venusand
Greenhorn

Joined: Aug 22, 2001
Posts: 7
What is the error that you see when you run your server ?
The error from client is obvious because server is not
running. So there is some problem with the server. The exact error message is not posted. please update.
venusand
Greenhorn

Joined: Aug 22, 2001
Posts: 7
one more thing to add. Start rmiregistry before starting server from the same dir. where server is run.
Marco Barenkamp
Ranch Hand

Joined: Aug 27, 2001
Posts: 94
The exception indicates that the host remotehost is not found.
Try the following line in your client app:
Calculator c =(Calculator) Naming.lookup( "rmi://localhost:1099/CalculatorService ");
I assume this will work.
Have fun!

Marco Barenkamp<br />_ _ _ _ _ ________________________ _ _ _ _ _ <br />L M I N T E R N E T S E R V I C E S AG<br /> <br />Head of Software Development<br /> <br /> <br />BEA Certified Enterprise Developer<br />Sun Certified Programmer for the Java2 Platform<br />Sun Certified Web Component Developer for the Java2 Platform<br />Sun Certified Developer for the Java 2 Platform <br />Sun Certified Business Component Developer for the Java 2 Platform <br />Sun Certified Enterprise Architect for the Java 2 Platform Enterprise Edition<br /> <br />LMIS AG
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
Hi,
Thanks a lot for your quick response.
I tried with rmi//localhost:1099...
It's working now. I am all set to study further chapters.
Thanks,
Padmashree
 
jQuery in Action, 2nd edition
 
subject: RMI problem
 
Similar Threads
RMI problem
UnMarshalException - Nested ClassNotFoundException again
Sun's RMI tutorial - Help !
Read a file line by line
can not found interface class