Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hello World RMI Example - With Dynamic Class Loading

 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the areas I have not worked on before is RMI. I found it quite messy getting an simple example working, especially in Eclipse where I prefer to do my development; so maybe this will be of use to others.

After messing around a bit with RMI I've finally got myself an "Hello World" RMI example running in Eclipse.

To run this example simply do the following:

1) Download the file (see below).
2) Unzip the file.
3) Follow the README.txt file in the unzipped folder.

This example uses a simple webserver called NanoHTTPD in order to serve the files for dynamic class loading.

I've tested this by running webserver, server, and client all on the one machine. I've also tested this with the webserver and server running on one machine, and the client running on a separate machine. Both setups work for me.

To confirm that the class files are actually being dynamically loaded there are three projects - the project containing the client requires the class file SwissWatch, but this class does not exist in the project for the client.

The three projects have files in common, such as Clock.java and Make.java - so if you intend modifying some of the files in one project then you may need to copy them to the other two projects.

Another little tip - if you look at the console output for the webserver you can see that it is receiving requests for the SwissWatch.class when a client runs. This is due to the dynamic class loading.

Hopefully this will be of some use to other RMI newbies

[edit] I added the file as an attachment.
Filename: rmi-example.zip
Description:
File size: 38 Kbytes
[Download rmi-example.zip] Download Attachment
 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly I just followed the RMI tutorial of Sun and had no difficulties to get the example working (using Eclipse also).

I remember some thread in this forum about someone also using this webserver for serving some files, but can't find the thread itself. The main difficulty was to run the program from the correct directory so the necessary classes could be served.
But to be honest I don't see why you would want to try this example with a seperate web server for serving files Except as a learning experience of course, because it adds some complexity you don't need for the assignment.
 
mohamed ramadan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, i found this example so useful to me in order to start with RMI, but after i've done all the steps and got both the server and webserver up and running and tried to run the client to start communicating with the server i 've got the following exception on the server console:

Waiting for invocations from clients...
Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.195.128:51716 accept,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkAccept(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

and on the client console this one :

Exception in thread "main" java.security.AccessControlException: access denied (java.net.SocketPermission 192.168.195.128:9000 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at org.metricspaces.rmi.Client.main(Client.java:24)

and thanks again for sharing that useful example.




 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some permission problems? Ensure that client.policy is on the classpath of your RMI_EXAMPLE_CLIENT project. Ensure that you followed step 3 in README.txt. As a wild shot in the dark - turn off any firewall, ensure that you are using the same port in the client and server for the registry.
 
mohamed ramadan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do i have to do with the client.policy ? the read me file doesn't contain steps regarding that.
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed ramadan wrote:what do i have to do with the client.policy ? the read me file doesn't contain steps regarding that.


You just need to ensure it exists in your Eclipse project - in the root of the project. Then it should be on the classpath. If it is not on the classpath then you won't be able to connect to the server.
 
mohamed ramadan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I made sure that the file is in the project directory, but i didn't get you with classpath step, is it meant that classpath in windows>enviroment variables or the .classpath for the project itself ? please clarify more this step because i'm very new to the RMI and it's configuration. thanks
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classpath for the project. Importing the project should ensure that it exists on the classpath. But you could always try adding it explicitly to the classpath in Eclipse.
 
mohamed ramadan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:The classpath for the project. Importing the project should ensure that it exists on the classpath. But you could always try adding it explicitly to the classpath in Eclipse.


I imported that file again to my project directory and appearing on eclipse well but unfortunatly still the access is denied although, all the steps are rechecked well. I frustrated
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe these guys here can help?

 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:Hopefully this will be of some use to other RMI newbies

I still don't understand why this would be of some use to a RMI newbie. It's overcomplicating RMI (because you don't need some web server serving the class files to get RMI to work). Why can a newbie not simply follow Oracle's RMI hands-on tutorial? I used it (and I was a newbie too) and it worked like a charm with no extra complexities to be added (like NanoHTTPD web server, client.policy file which should be on the classpath,...)
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:I still don't understand why this would be of some use to a RMI newbie.


It depends on your objectives. If all you want to do is learn the minimum required for OCMJD - then it may not be of much use. If you want to learn about RMI in general. Then it would be of use. Setting up RMI to use dynamic class loading and getting it working in Eclipse is not a trivial task.
 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:If all you want to do is learn the minimum required for OCMJD - then it may not be of much use. If you want to learn about RMI in general. Then it would be of use.

Agreed
 
mohamed ramadan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Keane wrote:Maybe these guys here can help?


Thanks alot dear for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic