//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();
}
}
}
software girl 0 Newbie Poster
Recommended Answers
Jump to Postand 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.
Jump to Postalso: 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.
Jump to Post@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 …
All 12 Replies
software girl 0 Newbie Poster
software girl 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
software girl 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
software girl 0 Newbie Poster
edensigauke -8 Light Poster
jwenting 1,905 duckman Team Colleague
stultuske 1,116 Posting Maven Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
software girl 0 Newbie Poster
jwenting 1,905 duckman Team Colleague
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.