//hello.java
import java.rmi.*;

    public interface Hello extends java.rmi.Remote

    String sayHello() throws RemoteException;
}
//HelloImpl.java
import java.rmi.Naming; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 
public class HelloImpl extends UnicastRemoteObject implements Hello 
{ 
  public HelloImpl() throws RemoteException {} 
  public String sayHello() { return "Hello world!"; } 
  public static void main(String args[]) 
  { 
    try 
    { 
      HelloImpl obj = new HelloImpl(); 
      // Bind this object instance to the name "HelloServer" 
      Naming.rebind("HelloServer", obj); 
    } 
    catch (Exception e) 
    { 
      System.out.println("HelloImpl err: " + e.getMessage()); 
      e.printStackTrace(); 
    } 
  } 
} 
//HelloClient.java
import java.rmi.RMISecurityManager; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 
public class HelloClient 
{ 
  public static void main(String arg[]) 
  { 
    String message = "blank"; 
    // I download server's stubs ==> must set a SecurityManager 
    System.setSecurityManager(new RMISecurityManager()); 
    try 
    { 
      Hello obj = (Hello) Naming.lookup( "//" + 
      "lysander.cs.ucsb.edu" + 
      "/HelloServer");         //objectname in registry 
      System.out.println(obj.sayHello()); 
    } 
    catch (Exception e) 
    { 
      System.out.println("HelloClient exception: " + e.getMessage()); 
      e.printStackTrace(); 
    } 
  } 
} 

Recommended Answers

All 12 Replies

i am a beginner in this topic..i wrote three files namely hello.java helloimpl.java and helloclient.java on javaeditor but now duunot know about how to run the rmic java policy etc....kindly gemme sm info can do next steps now??so that develop good understanding rmis

What exactly are you saying here? Sounds like "I don't like any of the tutorials on the web, so write one just for me"?

Anyway - you don't need to worry about make files for this simple example. Just compile all your classes with javac.exe, run the server and run the client with java.exe

well u answered my question in your last line...but not much!well the tutorials didn't develop an understanding..coz each tutorial says different after writing these three files..em not sure n got confused. i wrote my three files,but donot know how to compile them ?on command prompt??how??which statemens i have to write..??what's java policy file..these are my questions sir james!

I don't want to upset you, but RMI is an advanced Java topic, and if you are unsure about how to compile a java file then you are not ready for RMI yet. I recommend that you set RMI aside for now, and work the basics first. Then come back to RMI later.

sir i compile my java programs as...
cd\
cd java
javac <filename>
java <filename.java>

now i want to know about the policy file...n about RMI...i am trying my best to understand it..

Sorry Software girl, but the proper way to compic is using rmic filename, not by javac, I had that same problem also.

and you're wrong. You have to use rmic to create the stub classes which then are compiled together with the rest of the code using javac.

also: you should review your methods of "compiling"...
what you call compiling is both compiling and running the application, and both the examples you've shown are wrong.

do yourself a favour, don't start with RMI just yet.

Stub classes and rmic are not relevant!

That stub class / rmic stuff went the way of the dodo years ago (obsolete as of Java 1.5 - 2004!). Unfortunately nobody went through the internet deleting all the old obsolete posts about pre 1.5 RMI.
Current RMI uses reflection and there's no special build or pre-compile stages. Check out the Oracle tutorials

"Note: With versions prior to Java Platform, Standard Edition 5.0, an additional step was required to build stub classes, by using the rmic compiler. However, this step is no longer necessary. "

thnksss to all of yew!now i have a clear cut idea n concept of how run and compile a rmic program...

@James shows how long ago it is I last used RMI, during my preparations for the 1.5 SCJD assignment around that same time which still required generating stubs using rmic (the requirements hadn't been updated from 1.4 yet).

Never used RMI before or since, except implicitly in EJB2 calls and there it's blissfully hidden by the application server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.