//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(); 
    } 
  } 
} 

Dani AI

Generated

Quick practical answer that fills the gaps in the thread: modern Java RMI does not require rmic (static stub generation) for normal use — Java has supported dynamic stub creation since Java 5, so you can compile with javac and run with java without an extra rmic step. (docs.oracle.com)

Minimal workflow (safe for a local "Hello" test):

  • Compile the interface, server and client with javac and put the classes in a directory (e.g. classes).

  • Either start the registry as a separate process from the server-classpath directory, or start it inside your server JVM. The in-process approach avoids classpath headaches; add this before you bind:

    // inside server main before Naming.rebind(...)
    java.rmi.registry.LocateRegistry.createRegistry(1099);
  • Start the server (it binds the remote object) and then start the client (it calls Naming.lookup and invokes the method). Programmatic registry creation and the LocateRegistry API are the usual modern approaches. (docs.oracle.com)

Policy and security manager notes:

  • java.rmi.RMISecurityManager is deprecated; if code downloading or a security manager is needed, use new SecurityManager() (or supply a policy file). Do not rely on RMISecurityManager in current JVMs. (docs.oracle.com)

  • A minimal test policy (only for local testing) looks like:

    grant {
      permission java.security.AllPermission;
    };

    Run with -Djava.security.policy=server.policy if you enable a security manager. Replace AllPermission with fine-grained permissions before any real deployment. (docs.oracle.com)

Common troubleshooting (why ClassNotFoundException or stub errors happen):

  • Make sure the remote interface and any parameter/return types are on the client classpath; remote classloading is restricted by default (the JVM option java.rmi.server.useCodebaseOnly was hardened in recent updates), so do not rely on automatic downloading unless you understand codebase/security implications. (docs.oracle.com)

This patch-up clarifies the confusion raised by and ties together the correct points from , , and .

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.