0% found this document useful (0 votes)
51 views8 pages

Java RMI Tutorial Using Eclipse

This document provides a tutorial on using RMI (Remote Method Invocation) in Java to create a simple client-server application using Eclipse IDE. It describes how to: 1. Create a remote interface and implementation class on the server side. 2. Run an RMI registry and instantiate the remote object on the server. 3. Lookup and invoke the remote object from the client side. The key steps are to generate stubs for the remote object using rmic, start the RMI registry, run the server, and copy the stub file to the client project so it can locate and invoke the remote object.

Uploaded by

Demisew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Java RMI Tutorial Using Eclipse

This document provides a tutorial on using RMI (Remote Method Invocation) in Java to create a simple client-server application using Eclipse IDE. It describes how to: 1. Create a remote interface and implementation class on the server side. 2. Run an RMI registry and instantiate the remote object on the server. 3. Lookup and invoke the remote object from the client side. The key steps are to generate stubs for the remote object using rmic, start the RMI registry, run the server, and copy the stub file to the client project so it can locate and invoke the remote object.

Uploaded by

Demisew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EJB Tutorial

New Easy Tutorial for Java RMI using Eclipse


Posted by Imed Bouchrikaon November 12, 2016in Java RMI, ProgrammingNo Comments
This is a simple tutorial without using security policy and codebase property

In this RMI Programming tutorial, you will be using Eclipse IDE to learn how to create :

o Simple Remote Object.


o Server to instantiate (create ) and bind a remote object.
o Client to invoke remotely an object
As RMI is a Java to Java only communication protocol, you need to install the Java Development Kit ( JDK )
as well as a programming editor ( IDE ) such as Eclipse. For more details of how to install JDK and Eclipse,
Use the following tutorial:

Java Programming : Creating a simple java project using Eclipse

The structure of the files for the projects created using Eclipse throughout this tutorials is shown below:

1. Server Side
1 Let’s create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and call
it : RMIServerSide-> Click Finish once done
2 Under the RMIServerSide, project, Select New -> Interface

3 Set the name for the interface as : AdditionInterface –>Click Finish.

4 Copy the following code into the AdditionInterface code.


1. import [Link].*;
2.  
3. public interface AdditionInterface extends Remote {
4. public int add(int a,int b) throws RemoteException;
5. }
5 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : Addition

6 Copy the following code into the Addition class.


1. import [Link].*;
2. import [Link].*;
3.  
4. public class Addition extends UnicastRemoteObject
5. implements AdditionInterface {
6.  
7. public Addition () throws RemoteException { }
8.  
9. public int add(int a, int b) throws RemoteException {
10. int result=a+b;
11. return result;
12. }
13. }
7 Select the project RMIServerSide, Click New -> Class, Set the name for the class as : AdditionServer
8 Copy the following code in the AdditionServer
1. import [Link].*;
2. import [Link].*;
3.  
4. public class AdditionServer {
5. public static void main (String[] argv) {
6. try {
7. Addition Hello = new Addition();
8. [Link]("rmi://localhost/ABC", Hello);
9.  
10. [Link]("Addition Server is ready.");
11. }catch (Exception e) {
12. [Link]("Addition Server failed: " + e);
13. }
14. }
15. }
9 Compile your project through clicking Green Play button.

No worries if you get the following error, cause the rmiregistry is not running yet

10 Open your CMD ( DOS Console ).


11 Navigate to the bin folder of your project. The location of your project can be known through clicking:
Select the project RMIServerSide,, click : File -> Properties

Within the CMD black window, type in the full location of the bin folder as :

cd C:\Users\imed\workspace\RMIServerSide\bin

Make sure you type YOUR location, not mine

Press Enter once done.


12 Run the rmic to generate the stub for the remote object Addition. Run the following command:
rmic Addition -> Press Enter

If you get the message : ‘rmic’ is not recognized…. as shown below. You need to get the path configured
to add the bin folder for the JDK

Once you have used the rmic compiler, you will find the generated stub in the same folder:
13 In the same CMD window start the RMI Registry. Type in directly.
start rmiregistry

14 Now, time to compile and run the AdditionServer ! Click the green button:

The addition server is ready now !

2. Client Side
1 Let’s create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and call
it : RMIClientSide-> Click Finish once done
2 Select the project RMIClientSide, Click New -> Interface, Set the name for the class
as : AdditionInterface, Click Finish.
3 Copy the previous code into the AdditionInterface which is :
1. import [Link].*;
2.  
3. public interface AdditionInterface extends Remote {
4. public int add(int a,int b) throws RemoteException;
5. }
4 Select the project RMIClientSide, Click New -> Class, Set the name for the class as : AdditionClient,
Click Finish.
5 Copy the following code into the AdditionClient class
1. import [Link].*;
2.  
3. public class AdditionClient {
4. public static void main (String[] args) {
5. AdditionInterface hello;
6. try {
7. hello = (AdditionInterface)[Link]("rmi://localhost/ABC");
8. int result=[Link](9,10);
9. [Link]("Result is :"+result);
10.  
11. }catch (Exception e) {
12. [Link]("HelloClient exception: " + e);
13. }
14. }
15. }
6 Click on the Green Button again to [Link]’s it for the client side ? you would get the following error !
HelloClient exception: [Link]: error unmarshalling return; nested exception is:
[Link]: Addition_Stub (no security manager: RMI class loader disabled)

This is because there is no stub file on the client side.


7 Visit the bin folder of Server side where you have generated the stub file using the rmic compiler. To browse
directly, right click on the server project, click on Show In, and click on System Explorer

8 Copy the file (on the usb drive in case you want to run the client on a remote machine).

9 Browse to the bin folder of the client and paste the stub file inside the bin folder :
10 Compile and run again ! The result should shown on the console window of Eclipse.

Common questions

Powered by AI

To set up a simple Java RMI application using Eclipse, you must first install the Java Development Kit (JDK) and an IDE such as Eclipse. Begin by creating a server-side project and a client-side project. On the server side, create an interface (e.g., AdditionInterface) extending Remote and a class (e.g., Addition) implementing this interface. The server (e.g., AdditionServer) should create and bind the remote object. Compile the server with the rmic compiler to generate the remote object's stub and start the 'rmiregistry'. On the client side, create a similar interface and a class (e.g., AdditionClient) to invoke the remote methods using Naming.lookup(). Ensure the stub file is available on the client side to avoid unmarshalling exceptions during runtime .

A developer might choose to use the Java RMI framework because it provides seamless integration for remote method invocation across Java applications, leveraging Java's established object model and type safety. It simplifies network communication by abstracting the complexity of low-level TCP/IP socket programming, handling object serialization and remote method invocations natively. RMI also supports distributed garbage collection, making it easier to develop robust distributed systems compared to manual management required with other protocols. Additionally, RMI is strictly Java-centric, offering deep integration with Java's security model, which can be a significant advantage for Java-based environments .

Eclipse IDE offers several advantages for developing Java RMI applications. It provides a robust and feature-rich environment with tools for debugging, project management, and version control, which facilitates efficient and organized development. The IDE allows easy project setup, class creation, and integration with the Java Development Kit (JDK). Additionally, with features like code auto-completion and in-built refactoring tools, Eclipse streamlines the coding process, reducing development time and errors, making it particularly beneficial for managing complex Java RMI projects .

Generating a stub file on the client side is necessary in a Java RMI application because the stub acts as a client-side proxy for the remote object. It facilitates method invocations on the remote object by implementing the same set of remote interfaces that the remote object does, and handling the details of network communication. Without the stub, the client would encounter errors, such as java.rmi.UnmarshalException, due to the lack of a Java class definition for the serialized remote object references, leading to ClassNotFound exceptions .

If the 'rmic' command is not recognized while generating a stub in Java RMI, it typically indicates a problem with the system's PATH configuration. To resolve this, ensure that the Java Development Kit (JDK) bin directory is correctly added to the system PATH environment variable. This can be done by locating the JDK installation folder, copying the path to the bin directory, and adding it to the PATH variable in the system's environment settings. Once configured, the command should be executable from the command line .

In a Java RMI application, 'AdditionInterface' serves as a remote interface defining the method ('add') signature that can be invoked remotely, ensuring compliance with the contracting model of RMI. 'Addition' is the implementation class that provides the actual logic for the remote method ('add'), extending UnicastRemoteObject to facilitate remote method invocations by making it a remote object. 'AdditionServer' acts as the host server, responsible for instantiating the 'Addition' object, binding it to a name in the RMI registry, and making it available for remote clients to access the functionality offered by 'Addition' .

The 'rmiregistry' command starts the RMI registry, a crucial component of the Java RMI framework. It is a simple remote object registry that enables clients to locate and obtain stubs of remote objects to invoke their methods. When a server binds a remote object to a name using Naming.rebind or Naming.bind, it registers the object with the RMI registry running on a particular port. Clients can then use Naming.lookup with the respective name to retrieve the stub from the registry, facilitating remote method invocations .

Using a Remote Interface in Java RMI enhances security and design architecture by enforcing a clear contract between the client and the server. It defines the methods that can be invoked remotely, ensuring that only permitted operations are exposed for remote invocation, limiting the server's attack surface. This separation of interface and implementation allows for a modular architecture, providing flexibility in swapping implementations without affecting clients, thus promoting better design through interface-based programming and reducing dependencies .

A 'java.rmi.UnmarshalException' typically arises when the client encounters issues in deserializing a remote object's return value. This can occur if the client's runtime environment lacks the necessary class definitions, such as the stub class generated by 'rmic', which is essential for deserialization. Another cause can be the absence of a correct security manager setup, especially if the RMI class loader is invoked to load required class files over the network. Ensuring that stub classes are accessible on the client side and that security policies allow the necessary permissions can resolve this exception .

Java RMI accommodates differing Java versions between client and server by utilizing the serialized form of the remote object, designed to maintain backward compatibility across releases. However, compatibility issues can still arise if the client and server JVMs differ significantly in serialization or remote interface compatibility. To mitigate this, developers should ensure that remote interfaces remain stable and serialization versions are controlled using serialVersionUID to maintain compatibility. Adhering to consistent Java standards and avoiding the use of newer language features not supported by older JVMs also helps in maintaining compatibility across different Java versions [No specific source provided in original document].

You might also like