RMI (iii) -- Java RMI simple example

Source: Internet
Author: User
From: http://blog.csdn.net/ladofwind/archive/2005/01/11/248820.aspx

RMI is the specification for remote calls on the Java platform. The following is a small example.

There are three Java classes, remote interfaces, ServerProgram, Client program

Remote interface:

Import java. RMI .*;

Public interface helloin extends java. RMI. Remote {
String sayhello () throws RemoteException;
}

Server program:

Import java. RMI .*;
Import java.net .*;
Import java. RMI. Registry .*;
Import java. RMI. server .*;

Public class Hello extends java. RMI. server. unicastremoteobject implements helloin {
Public Hello () throws RemoteException {
Super ();
}
Public String sayhello () throws RemoteException {
Return "Hello, world! ";
}
Public static void main (string [] ARGs ){
// System. setsecuritymanager (New java. RMI. rmisecuritymanager ());
Try {

Hello H = new Hello ();
Java. RMI. Naming. rebind ("hello", H );
System. Out. Print ("ready ......");
}
Catch (exception e ){
E. printstacktrace ();
}

}
}

Run the RMI registration program start rmiregistry in the command line mode before running the server program.

Client Program:

Import java. RMI .*;
Import java. RMI. Registry .*;

Public class helloworld {
Public static void main (string [] ARGs ){
// System. setproperty ("Java. Security. Policy", "client. Policy ");
// System. setsecuritymanager (New java. RMI. rmisecuritymanager ());
Try {
Helloin HI = (helloin) Naming. Lookup ("// fengl/Hello ");
For (INT I = 0; I <10; I ++ ){
System. Out. println (Hi. sayhello ());
}
}
Catch (exception e ){
E. printstacktrace ();
}
}
}

Before executing the client program, use rmic hello to generate stub and skeleton classes.
It is actually the underlying Implementation of Remote calls.

Finally, execute the Java helloworld console to print Hello, world, and call it successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.