• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

RMI problem

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing to add. Start rmiregistry before starting server from the same dir. where server is run.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic