Old Linux Admin 2008 -->
Home Debian Install + WIFI WIFI Install cont'd Debian Install Done! Cisco Soho97 Config LinuxCmds Firewalls AntiVirusInfo.html Shorewall Firewall Old Linux Admin Series Java RMI Asterisk_PBX_Info Databases - A Research Paper VPN PPTPD Info Aloha Packet Radio Transmission Apache2_Info.html BasicSecurityConcepts.html C_and_Java_Info.html CV.html Fetchmail_SSL_Info.html Hard_Drive_Info.html LVM2_Volume_Info.html MYSQL_Info.html Networking_Info.html Packet_Inspection_Info.html Security_Info_Wireshark_WIFI.html Snort_Info.html Subnet_Mask_Info.html Useful_One_Liners.html New DebianAdmin Site
Java Remote Method Invocation (RMI)

Network Server-Client connectivity is possible in Java programs via the RMI library functions.

A simple example showing a Server-Client session follows:

The code can be cut and pasted into Netbeans to check and compile it, or copied straight to text files and compiled on the cmd line in Linux or Windows if the the Sun JDK environment is installed.

As Plymouth University uses Netbeans IDE for this tutorial, I will go through this process first as this IDE App is new to me, and I am NOT a programmer to say the least! Java with BlueJ nearly finished me off in the 2nd year FdSc...

Once the Netbeans IDE is installed, a new Project can be started, called HelloWorld on both the Server and Client PCs for ease of copying the compiled class files to the Program686 JDK directory in Windows later, on each PC, where ther will be run from.

 

The pre-written code from the University PDF is below, for each file:

Define an RMI Service Interface

Any system that uses RMI will have to use a service interface. This service

interface is very simple and its main job is to define a list of object interfaces

that can be invoked remotely. So in our case, we would like to have a method

called printHello(). The implementation of this method will be in our server

and we will come to that later. For time being, what we need is to include the

definition of this interface, i.e. we need to advertise the service that the server

offers. The listing for the service interface (HelloWorld.java) definition is as

follows.

 

HelloWorld.java

import java.rmi.Remote;

import java.rmi.RemoteException;

 

 

public interface HelloWorld extends Remote {

String printHello() throws RemoteException;

}

 

Client Implementation

A client program obtains a stub for the registry on the server�s host. It then

looks up the remote object�s stub by name in the registry. After the remote

object�s stub is found, any advertised interface (in this case printHello())

can be invoked remotely. The source listing of the client code (Client.java)

is as follows.

 

Client.java

import java.rmi. registry. Registry;

import java.rmi. registry. LocateRegistry;

 

public class Client {

 

private Client () {}

 

public static void main( String [] args) {

 

String host = ( args. length < 1) ? null : args [0];

try {

Registry registry =

LocateRegistry. getRegistry( host );

HelloWorld stub =

( HelloWorld) registry. lookup (" HelloWorld");

String response = stub. printHello();

System .out. println(" response: " + response);

} catch ( Exception e) {

System .err. println(" Client exception: " +

e. toString());

e. printStackTrace();

}

}

}

 

----------------------------------------------------------------------------------------------------------

 

RMI Service Implementation

Once the RMI service interface is defined, we are now ready to implement the

service interface. We can of course implement many service interfaces, but only

those defined in the RMI interface can be accessed remotely. Below is the listing

of the implementation of the RMI service interface (HelloWorldImpl.java).

 

HelloWorldImpl.java

import java.net .*;

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

 

public class HelloWorldImpl

extends java.rmi.server.UnicastRemoteObject

 

implements HelloWorld {

 

public HelloWorldImpl()

throws java.rmi. RemoteException {

super ();

}

 

public String printHello()

throws java.rmi. RemoteException {

String str;

try {

InetAddress host =

InetAddress. getLocalHost();

str = " from " +

host. getHostAddress (). toString();

} catch ( UnknownHostException e) {

str = "";

}

return " Hello World " + str;

}

}

 

Creating an RMI Server

The RMI server has two functions:

to create an instance of a service implementation, and

to register the instance with an RMI registry (rmiregistry).

This server could be combined with the RMI service implementation, but sep-

arating these two results in a cleaner code design and this is what we will do

here.

For a client to be able to invoke a method on a remote object, that client

must first obtain a stub for the remote object. Java RMI registry is name service

(mini DNS) that allows a client to get a stub (reference) to a remote object.

Once a remote object is registered on a server, clients can look up the object

by name, obtain a remote object reference and invoke remote methods that the

object advertises.

The listing of the server (HelloWorldServer.java) is as follows.

 

HelloWorldServer.java

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

 

public class HelloWorldServer {

 

 

public static void main( String args []) {

System.out.println(" Loading RMI service");

 

try {

HelloWorldImpl stub = new HelloWorldImpl();

String name = HelloWorld. class. getSimpleName ();

Registry registry = LocateRegistry.getRegistry();

registry.bind(name , stub );

 

System.err.println(" Server is ready ");

}

catch ( Exception e) {

System.err.println(" Server exception: " + e.toString());

e. printStackTrace();

}

}

--------------------------------------------------------

 

A new project is opened in Netbeans IDE:

 

Choose Java Project/JavaApplication, next, then name the Project �HelloWorld� � whatever � and Finish.

This creates a required �main� Class for the Project.

Right click the HelloWorld package and choose New Empty File and call it HelloWorld also. This will be the first file in the list above.

Cut and paste the code above from HelloWorld.java into this empty file.

Repeat this process for the rest of the code sections above, and check that Netbeans has no problems with missing pasted code, a lost �{� or other.

BE CAREFUL NAMING FILES! It can be problematic to undo/rename inconsistencies such as Upper Case names later, if a folder has the wrong spelling etc. as the files may not compile.

If you make a mistake in file naming or other, you can try Refactoring, by R-clicking the problem file, or copying the files to another Project, deleting the problem project then renaming the new one � hassle! Best to get it right first time...

 

If all is well, no red warnings should show in the left column of the code, or next to the file name.

You will have 5 files when done including Main.

Check they build by clicking the green Run button, and if all is well, you should have

BUILD SUCCESSFUL (total time: 0 seconds)

But maybe with errors when run such as:

run:

java.lang.NoClassDefFoundError: helloworld/Main (wrong name: HelloWorld/Main)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

at java.net.URLClassLoader.access$000(URLClassLoader.java:56)

at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

Could not find the main class: helloworld.Main. Program will exit.

Exception in thread "main" Java Result: 1

 

As I�m only using Netbeans to for self familiarity and to show code errors for this example, I`m not interested in this output, as the Client file needs to be run on a seperate PC than the Server, and the rmiregistry.exe service has to be running also, which will be done from the Windows Command prompt, after compilation of the code.

All the .java files can be found in Win7 in:

C:\Users\your_dir\Documents\NetBeansProjects\HelloWorld

Which you can find by R-clicking the Projects Properties.

They can be copied to the JDK directory, which in Win7 is:

C:\Program Files (x86)\Java\jdk1.6.0_14\bin

And in my case for XP, where I will run the Server from, is:

E:\Program Files\Java\jdk1.6.0_14\bin

Once copied to the relevant folders on each PC, they can be compiled using javac.exe, and the rmiregistry.exe started, then the HelloWorldServer.class file run after, on the Server PC:

CompileServer.JPG

StartRMIexe.jpg

When started OK, the cmd screen remains blank:

StartRMI.jpg

RMIServerReady.jpg

In the same way, the HelloWorld.java, and Client.java files can be compiled on the Client PC.

The java Client.class file can then be run, appending the IP address of the Server PC as an argument, to connect to the Server PC at its IP address, and call the Object that returns the output �HelloWorld�

An in depth tutorial on this example of Java RMI code can be found at:

http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html

Steve 2008-2022