1. First write the client and server and algorithm code. The code is as follows:
(1) Interface code
Package com.wonder.rmi;
Import Java.rmi.Remote;
Import java.rmi.RemoteException;
Import java.util.List;
Public interface Rmioperate extends Remote {
Public double Add (double x,double y) throws remoteexception;
Public double minus (double x,double y) throws remoteexception;
Public double Add (List L) throws remoteexception;
}
(2) Implementation of the interface algorithm
Package com.wonder.rmi.server;
Import java.rmi.RemoteException;
Import Java.rmi.server.UnicastRemoteObject;
Import java.util.List;
Import Com.wonder.rmi.RMIOperate;
public class Operateimpl extends UnicastRemoteObject implements Rmioperate {
Public Operateimpl () throws RemoteException {
Super ();
}
/**
*
*/
Private static final long serialversionuid = 1L;
Public double Add (double x, double y) throws RemoteException {
Double z=x+y;
return z;
}
Public double minus (double x, double y) throws RemoteException {
Double z=x-y;
return z;
}
Public double Add (List L) throws RemoteException {
Return double.parsedouble (string.valueof (double) l.get (0) + (double) L.get (1));
}
}
(3) server-side programs
Package com.wonder.rmi.server;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
public class Rmiserver {
/**
* @param args
*/
public static void Main (string[] args) {
try {
Operateimpl o = new Operateimpl ();
System.out.println ("Server is starting ...");
Locateregistry.createregistry (7099); This port should be the same as the bound port, if not specified, the default port is 1099
Naming.rebind ("Rmi://192.168.71.214:7099/rmioperate", O); This port is the same as the port above
System.out.println ("Waiting RMI client Invoke ...");
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
2. Client program
Package com.wonder.rmi.client;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import java.rmi.NotBoundException;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
Import Java.rmi.registry.Registry;
Import java.util.ArrayList;
Import java.util.List;
Import Com.wonder.rmi.RMIOperate;
public class Rmiclient {
/**
* @param args
*/
@SuppressWarnings ({"Unchecked", "unchecked"})
public static void Main (string[] args) {
String host = "192.168.71.214";
try {
Calling Mode 1
Registry reg = null;
reg = Locateregistry.getregistry ("192.168.71.214", 7099);
Rmioperate rmiobj = (rmioperate) reg.lookup ("Rmioperate");
Calling Mode 2
Rmioperate rmiObj1 = (rmioperate) naming.lookup ("Rmi://192.168.71.214:7099/rmioperate"); To find the port for a remote server
Double x = 1;
Double y = 2;
List L = new ArrayList (2);
L.add (0,double.valueof ("123"));
L.add (1,double.valueof ("456"));
Double z = rmiobj.add (x,y);
System.out.println ("add=" +z);
Double w = rmiobj.minus (x,y);
System.out.println ("minus=" +w);
Double _a = Rmiobj.add (l);
System.out.println ("Add list=" +_a);
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Notboundexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
3. Generate stub
c:/> Java weblogic.rmic Com.wonder.rmi.server.OperateImpl
To facilitate testing, make a jar package and put it into the startweblogic.cmd path
4. If you call Locateregistry in your code, you do not have to start the rmregistry command at the command Prompt window
5. Set WebLogic startup class, Com.wonder.rmi.server.RMIServer. It's OK to start WebLogic without error.
Two. A common problem
1. If there is a problem with connection rejection, check that RMI registration is starting normally. and the binding address and port are correct.
2. If a stub class is not found, check to see if the stub class was generated using Rmic. You can also check to see if the port is set correctly.