Java RMI Simple Example

Source: Internet
Author: User
Tags interface string client
The example RMI is the specification of a remote invocation of the Java platform, and here is a small example of the native test passing

A total of three Java classes, remote interfaces, server-side programs, client programs

Remote interface:

Import java.rmi.*;

Public interface Helloin extends java.rmi.remote{
String SayHello () throws remoteexception;
}

Service-Side programs:

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

}
}

Start the RMI registration program on the command-line before executing the server-side program: Start Rmiregistry

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

Use Rmic Hello to generate stub and skeleton class before executing the client program, which
is actually the underlying implementation of the remote call.

Finally, execute the Java Helloworld console to print out the Hello,world, successfully invoked.



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.