EJB Tutorial: New Easy Tutorial For Java RMI Using Eclipse
EJB Tutorial: New Easy Tutorial For Java RMI Using Eclipse
EJB Tutorial: New Easy Tutorial For Java RMI Using Eclipse
In this RMI Programming tutorial, you will be using Eclipse IDE to learn how to create :
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
No worries if you get the following error, cause the rmiregistry is not running yet
Within the CMD black window, type in the full location of the bin folder as :
cd C:\Users\imed\workspace\RMIServerSide\bin
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:
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 java.rmi.*;
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 java.rmi.*;
2.
3. public class AdditionClient {
4. public static void main (String[] args) {
5. AdditionInterface hello;
6. try {
7. hello = (AdditionInterface)Naming.lookup("rmi://localhost/ABC");
8. int result=hello.add(9,10);
9. System.out.println("Result is :"+result);
10.
11. }catch (Exception e) {
12. System.out.println("HelloClient exception: " + e);
13. }
14. }
15. }
6 Click on the Green Button again to RUN.That’s it for the client side ? you would get the following error !
HelloClient exception: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: Addition_Stub (no security manager: RMI class loader disabled)
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.